您好我想将图片从网络网址保存到我的本地文件夹。不是我的代码
<?php
$url = 'http://dev.aviesta.com.mx/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/b/r/bridal-shoes1.jpg';
$saveto = '/var/www/';
function grab_image($url,$saveto){
$url = 'http://dev.aviesta.com.mx/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/b/r/bridal-shoes1.jpg';
$saveto = '/var/www/';
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
}
?>
我试过这段代码,但它对我不起作用请给我一些想法
答案 0 :(得分:1)
为什么要使用cURL?
$url = 'http://dev.aviesta.com.mx/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/b/r/bridal-shoes1.jpg';
$saveto = '/var/www/image_' . time() . '.jpg';
grab_image($url, $saveto);
function grab_image($url, $saveto) {
// Assumes a correctly encoded URL
$image = file_get_contents($url);
if (!$image)
return false;
file_put_contents($saveto, $image);
if (!file_exists($saveto))
return false;
return true;
}
还要确保检查Web服务器是否有权写入/ var / www /
答案 1 :(得分:1)
进行了一项更改,如下所示
allow_url_fopen = On
并取消后续行。
extension=php_curl.dll
$img = file_get_contents('http://graph.facebook.com/'.$response['user_id'].'/picture?type=large');
$image_name = time().".jpg";
/* putting image into the orignal images folder to keep the original image */
$upload_path = "img".DS."origional_images".DS."";
file_put_contents("img".DS."origional_images".DS.$image_name, $img);
我根据您发布的评论尝试过以下代码 不幸的是,在你的评论中,你有额外的'在网址链接的末尾摆脱额外',它将按预期工作。
$img = file_get_contents('http://dev.aviesta.com.mx/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/b/r/bridal-shoes1.jpg');
$image_name = time().".jpg";
$upload_path = "img/";
file_put_contents("img/".$image_name, $img);
答案 2 :(得分:0)
使用$fp = fopen($saveto, 'wb');
。