如何指向此图像下载到我的本地驱动器中,位于此C:/ Users / AlexBoey / Pictures / images /
$url_to_image = 'http://cleversoft.co/wp-content/uploads/2013/08/senior-php-developer.jpg';
$ch = curl_init($url_to_image);
$my_save_dir = 'C:/Users/AlexBoey/Pictures/images/';
$filename = basename($url_to_image);
$complete_save_loc = $my_save_dir . $filename;
$fp = fopen($complete_save_loc, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
答案 0 :(得分:1)
file_get_contents()
怎么样? http://php.net/manual/en/function.file-get-contents.php
$image = file_get_contents('http://cleversoft.co/wp-content/uploads/2013/08/senior-php-developer.jpg');
然后file_put_contents()
吗? http://php.net/manual/en/function.file-put-contents.php
file_put_contents('C:/Users/AlexBoey/Pictures/images/image.jpg', $image);
更新-如果您是从笔记本电脑本身运行PHP,则以上内容适用。事实并非如此。因此,除此之外,您还需要添加标题附件内容并回显内容($ file是服务器上保存的路径,将file_put_contents更改为服务器上的某个临时位置,您可以
unlink()
将其回显之前) :
$quoted = sprintf('"%s"', addcslashes(basename($file), '"\\'));
$size = filesize($file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);
// you can `unlink($file)` the image saved on the server
echo $image;