0

嗨,我想将图像从 web url保存到我的本地文件夹。下面是我的代码

<?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);
   }

?>

我已经尝试过这段代码,但它对我不起作用请给我一些想法

4

3 回答 3

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/

于 2013-01-07T05:17:46.717 回答
1

php.ini文件中进行了如下更改

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);

试过的代码

我根据您发布的评论尝试了以下代码。
不幸的是,在您的评论中,您在 url 链接末尾有多余的 ' 摆脱了多余的 ',它将按预期工作。

$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);
于 2013-01-07T06:06:22.070 回答
0

使用$fp = fopen($saveto, 'wb');.

于 2013-01-07T05:17:58.827 回答