php - 如何通过 php 将图像下载到我的笔记本电脑路径?
问题描述
如何在此 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);
解决方案
怎么样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;
推荐阅读
- c# - 无法在动态数据表中读取 Kendo Pivot Grid MVC 中未定义的属性“值”
- amazon-s3 - 使用 S3 签名 URL 到 PUT 对象时出现间歇性 403 CORS 错误
- redhat - 如何在 Red Hat Developer Studio 12.9.0.GA 中安装 Subversion?
- java - 使用缺少 .prj 文件的 GeoTools 从 UTM 转换为 WGS84?
- azure - 如何对 https://management.azure.com api 进行身份验证?
- python - 无法从 tensorflow.keras.metrics 导入指标
- php - 切换url时加载不同的视图文件
- java - 尝试从 oracle 11.2.0.3 升级到 oracle-12.1.0.2-32 时出错
- azure - 是否有将 CRM 链接到 Azure DevOps 的 API
- reporting-services - SSRS 2016 修复矩形的大小