PHP下载文件最简单的方法是什么
我需要从某个 URL 下载图像到我的服务器。但是,我的服务器的配置不允许我这样做:
getimagesize( $file );
因为,它会生成错误:
Warning: getimagesize() [function.getimagesize]: URL file-access is disabled in the server configuration in somefile.php on line 10
那么,我是否可以使用另一种不需要外部库的方法?
好的,我用 OIS 的解决方案解决了这个问题:
$filename = '/tmp/'.md5($file);
$ch = curl_init($file);
$fp = fopen($filename, "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$ext = getimagesize( $filename );
I need to download an image from some URL to my server. However, my server's config disallowed me to do it this way:
getimagesize( $file );
Because, it generate error:
Warning: getimagesize() [function.getimagesize]: URL file-access is disabled in the server configuration in somefile.php on line 10
So, is there another way I can use that doesn't require external library?
Ok, I solved it with solution from OIS:
$filename = '/tmp/'.md5($file);
$ch = curl_init($file);
$fp = fopen($filename, "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$ext = getimagesize( $filename );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试 curl。
You can try curl.
除非修改服务器配置,否则不可能
it is not possible unless you modify the server configuration
如果您的服务器上禁用了对 URL 的文件访问,则您无法从外部 URL 加载任何内容。这就是该配置的目的。
在未禁用它的服务器上,file_get_contents($url) 是获取大多数文件的最简单方法。
If file access to URLs is disabled on your server, you can't load any content from external URLs. That's the purpose of that configuration.
On a server where it's not disabled, file_get_contents($url) is the easiest way to fetch most files.
使用以下命令将文件下载到
$imgdata
中。然后您可以将其保存到文件并获取其图像大小。您可能可以通过一些工作来删除文件保存步骤,以使其更快。Use the following to download the file into
$imgdata
. You can then save it to a file and get its image size. You can probably remove the file saving step with a bit of work to make it faster.