【问题标题】:save images from url through php get content通过 php 从 url 保存图像获取内容
【发布时间】:2012-12-20 21:57:55
【问题描述】:
<?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);


?>

当我尝试这个时,我得到以下 错误

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

【问题讨论】:

  • 亲爱的我把这个问题放在这里是因为我不明白这个错误
  • Apache 是否拥有创建文件 /var/www/ajaxForm/loading.gif 的良好权限?
  • 是的,文件已经创建,apache有读写权限
  • 关于 SO 的类似问题:stackoverflow.com/questions/6306935/…
  • @RohitGoel 你看到下面的答案了吗。

标签: php image url save


【解决方案1】:

您需要保存该图像吗?我不知道你为什么写这段代码,你不能打开一个 gif 图像并在其中写入另一个。如果要保存该图像,则可以使用

file_put_contents("imgfolder/imgID.jpg", $image);copy($image_url, $your_path);

【讨论】:

  • 这很好,但我想将 url 中的图像保存在我的本地文件夹中,它应该会自动创建文件
【解决方案2】:

刚刚取自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));

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

参考资料:

file_put_contents

file_get_contents

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 2010-10-17
    • 2016-08-06
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    相关资源
    最近更新 更多