1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

laravel file_get_contents の代わりに Http を使用しリモートファイル画像を保存

Posted at

参考
https://qiita.com/akitkat/items/b3992ccb54e2944200cd

HogesController.php
<?php
//画像保存テスト
$img_path = "https://gahag.net/img/201608/27s/gahag-0119426971-1.jpg";
$img_binary = Http::withoutVerifying()
->get($img_path);

//            画像を取得できたら保存
if($img_binary->successful()){
//https://twikon.club/tmp/get.jpg
    file_put_contents(public_path("tmp/get.jpg"),$img_binary);//保存。 hoge.jpg などの画像ファイル名まで含めて保存
    print_r("画像を取得できました");
} else {
    print_r("エラーが発生しました");
}
?>

これでリモートファイルを保存することが可能です。

file_get_contents と違うところ

画像をダウンロードできない場合もsuccessfulで条件分岐ができるので楽。

以上

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?