Home > Backend Development > PHP Tutorial > 有一个图片的下载链接,php如何下载图片到本地

有一个图片的下载链接,php如何下载图片到本地

WBOY
Release: 2016-06-20 12:30:58
Original
810 people have browsed it

比如图片链接地址如下:http://125.35.6.80:8080/ftba/itownet/download.do?method=downloadFile&fid=917d9f52486042a4829ea9392b801f6d&ssid=KV_L24clMSHsqY7punwrgA||
使用PHP如何自动的下载到本地,我使用file_get_contents和curl两种方法均未成功。

我使用的方法代码如下:
1)file_get_contents:
$img = file_get_contents(‘http://125.35.6.80:8080/ftba/itownet/download.do?method=downloadFile&fid=917d9f52486042a4829ea9392b801f6d&ssid=KV_L24clMSHsqY7punwrgA||’);
file_put_contents('1.jpg',$img);

2)curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch,CURLOPT_URL,‘http://125.35.6.80:8080/ftba/itownet/download.do?method=downloadFile&fid=917d9f52486042a4829ea9392b801f6d&ssid=KV_L24clMSHsqY7punwrgA||’);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file_content = curl_exec($ch);
curl_close($ch);
$downloaded_file = fopen('1.jpg', 'w');
fwrite($downloaded_file, $file_content);
fclose($downloaded_file);

望指教,在此谢过。


回复讨论(解决方案)

curl 缺少:
 curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

file_get_contents 缺少第三个参数数:
$context = stream_context_create(array('http' => array('user_agent' => $_SERVER['HTTP_USER_AGENT']))); 

http://blog.csdn.net/shrimpma/article/details/9816035

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template