Php 通过curl将图像从url保存到本地文件夹

Php 通过curl将图像从url保存到本地文件夹,php,image,curl,save,Php,Image,Curl,Save,您好,我想将图像从weburl保存到我的本地文件夹。下面是我的代码 <?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:/

您好,我想将图像从weburl保存到我的本地文件夹。下面是我的代码

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

?>

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

为什么要使用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/

使用
$fp=fopen($saveto,'wb')做了如下更改

allow_url_fopen = On
并取消对下一行的注释

extension=php_curl.dll
或 试码 根据您发表的评论,我尝试了以下代码。
不幸的是,在你的评论中,你有额外的“在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);

请详细说明一下你的答案,我对如何使用你的答案感到困惑(“”,/var/www/keep fopen/enabled.png');你找到一个有效的答案了吗?这是一个完整的代码,它可以很好地工作,因为你可能在文件的末尾有“获取”内容。。。只要点击你们在评论中给出的链接,我打赌你们会看到的404@DipeshParmar,假设Rohit正在保存到
/var/www/image
意味着他正在使用Linux或Mac堆栈,为什么他需要在php.ini中取消对Windows dll的注释?为了使用额外的库,你需要在使用它之前启用它…就像函数一样…要调用任何函数,你必须先声明函数。@DipeshParmar dll文件是Windows专用的,php扩展名有一个
。所以在Linux/Mac中
扩展名
$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);