通过php保存网址中的图像获取内容

Roh*_*oel 2 php url image save

<?php
//Get the file
$content = 'http://dev.aviesta.com.mx/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/b/r/bridal-shoes1.jpg ';
//Store in the filesystem.
$fp = fopen('/var/www/ajaxForm/loading.gif', 'w'); 
fwrite($fp, $content);
fclose($fp);


?>
Run Code Online (Sandbox Code Playgroud)

我尝试这个时出现以下错误

警告:fopen(/var/www/ajaxForm/loading.gif):无法打开 :第5行/var/www/curlimage.php中没有这样的文件或目录警告:fwrite()期望参数1是资源,第6行/var/www/curlimage.php中给出的布尔值警告:fclose()要求参数1为资源,第7行的/var/www/curlimage.php中给出布尔值

diE*_*cho 6

刚刚从 similar question

$url = 'http://dev.aviesta.com.mx/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/b/r/bridal-shoes1.jpg';
$img = 'curl/curl_image.jpg';
file_put_contents($img, file_get_contents($url));
Run Code Online (Sandbox Code Playgroud)

注意:还要检查目录是否可以使用is_writable方法 写入

参考文献:

file_put_contents

file_get_contents