php - 通过php获取内容从url保存图片

标签 php image url 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);


?>

当我尝试此操作时出现以下错误

Warning: fopen(/var/www/ajaxForm/loading.gif): failed to open stream: No such file or directory in /var/www/curlimage.php on line 5 Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/curlimage.php on line 6 Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/curlimage.php on line 7

最佳答案

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

关于php - 通过php获取内容从url保存图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14191425/

相关文章:

php - 防止通过 PHP 表单将重复项添加到数据库表中

php - 用于测验的 MySql 数据库设计

php - 根据日期持续时间从mysql查询获取数据

javascript - Facebook 如何控制#!在网址中

php - 多对多关系 Laravel

java - 放大URL图像尺寸

html - 如何根据父级高度动态裁剪图像

javascript - 当我将新 URL 推送到 Backbone.history 时,查询参数会保留吗?

java - 将 Cookie 从 Java 传递到浏览器

android - 如何在 sqlite-android 中存储和检索图像