用curl PHP保存图片

我需要使用CURL从URL保存图像,并将其保存到我的服务器上的文件夹。 我一直在与这个代码作斗争,无济于事。 理想情况下,我想抓住图像并将其保存为“photo1”或其他东西。 帮帮我!

function GetImageFromUrl($link) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch,CURLOPT_URL,$link); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result=curl_exec($ch); curl_close($ch); return $result; } $sourcecode = GetImageFromUrl($iticon); $savefile = fopen(' /img/uploads/' . $iconfilename, 'w'); fwrite($savefile, $sourcecode); fclose($savefile); 

尝试这个:

 function grab_image($url,$saveto){ $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); } 

并确保在php.ini中启用allow_url_fopen

选项1

您可以使用CURLOPT_FILE选项直接显示一个文件到curl进行下载,而不是将二进制/原始数据CURLOPT_FILE到variables中,然后编写。

这是function:

 // takes URL of image and Path for the image as parameter function download_image1($image_url, $image_file){ $fp = fopen ($image_file, 'w+'); // open file handle $ch = curl_init($image_url); // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // enable if you want curl_setopt($ch, CURLOPT_FILE, $fp); // output to file curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 1000); // some large value to allow curl to run for a long time curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); // curl_setopt($ch, CURLOPT_VERBOSE, true); // Enable this line to see debug prints curl_exec($ch); curl_close($ch); // closing curl handle fclose($fp); // closing file handle } 

下面是你应该怎样称呼它:

 // test the download function download_image1("http://www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2", "local_image1.jpg"); 

选项#2

现在, 如果你想下载一个非常大的文件 ,那么上面的情况可能不会变得方便。 您可以使用下面的函数来处理大文件。 此外,如果需要,您可以打印进度(以%或其他格式)。 下面的函数是使用一个callback函数来实现的,该callback函数将一大块数据写入到下载过程中的文件中。

 // takes URL of image and Path for the image as parameter function download_image2($image_url){ $ch = curl_init($image_url); // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // enable if you want curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 1000); // some large value to allow curl to run for a long time curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); curl_setopt($ch, CURLOPT_WRITEFUNCTION, "curl_callback"); // curl_setopt($ch, CURLOPT_VERBOSE, true); // Enable this line to see debug prints curl_exec($ch); curl_close($ch); // closing curl handle } /** callback function for curl */ function curl_callback($ch, $bytes){ global $fp; $len = fwrite($fp, $bytes); // if you want, you can use any progress printing here return $len; } 

这里是如何调用这个函数:

 // test the download function $image_file = "local_image2.jpg"; $fp = fopen ($image_file, 'w+'); // open file handle download_image2("http://www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2"); fclose($fp); // closing file handle 

改进的Komang答案(添加引用者和用户代理,检查是否可以写入文件),如果没有问题,则返回true;如果有错误,则返回false:

 public function downloadImage($url,$filename){ if(file_exists($filename)){ @unlink($filename); } $fp = fopen($filename,'w'); if($fp){ $ch = curl_init ($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); $result = parse_url($url); curl_setopt($ch, CURLOPT_REFERER, $result['scheme'].'://'.$result['host']); curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0'); $raw=curl_exec($ch); curl_close ($ch); if($raw){ fwrite($fp, $raw); } fclose($fp); if(!$raw){ @unlink($filename); return false; } return true; } return false; } 

这是最简单的实现。

 function downloadFile($url, $path) { $newfname = $path; $file = fopen($url, 'rb'); if ($file) { $newf = fopen($newfname, 'wb'); if ($newf) { while (!feof($file)) { fwrite($newf, fread($file, 1024 * 8), 1024 * 8); } } } if ($file) { fclose($file); } if ($newf) { fclose($newf); } }