$tempDir = '/download/file/here';
$finalDir = '/keep/file/here';
$imageUrl = 'http://www.example.com/image.jpg';
exec("cd $tempDir && wget --quiet $imageUrl");
if (!file_exists("$tempDir/image.jpg")) {
throw new Exception('Failed while trying to download image');
}
if (rename("$tempDir/image.jpg", "$finalDir/new-image-name.jpg") === false) {
throw new Exception('Failed while trying to move image file from temp dir to final dir');
}
9 回答
如果
allow_url_fopen
设置为true
:否则使用cURL:
在这里,示例将远程图像保存到image.jpg .
Vartec's answer与cURL对我不起作用 . 由于我的具体问题,它确实略有改善 .
e.g.,
当服务器上有重定向时(例如当您尝试保存Facebook Profiles 图像时),您将需要以下选项集:
The full solution becomes:
我无法使任何其他解决方案工作,但我能够使用wget:
见file()PHP Manual:
创建一个名为images的文件夹,该文件夹位于您计划放置要创建的php脚本的路径中 . 确保它具有每个人的写权限,否则脚本将无法工作(它将无法将文件上载到目录中) .