3

所以,我想安全地从 URL 下载图像。当然,我的第一个想法是检查扩展 - 如果它兼容,只需下载它并使用好的扩展保存它(它应该是安全的,因为服务器不会运行存储在.jpg文件中的 PHP 代码(好吧,除非它是配置为这样做))。使用这种方法是否还有一些风险,是否有更好的方法?

4

2 回答 2

0

您应该检查它是否是图像,因为可以操纵扩展名。

$file = file_get_contents($url);
if(@imagecreatefromstring($file)) { 
  // it's a valid image file; handle it
}
else {
  // it's not an image file; error!
}
于 2015-04-26T22:41:54.647 回答
0
/* gets the data from a URL */
function get_data($url) {
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

要获取文件信息,您可以在此处curl_getinfo()使用 更多信息

这应该可以帮助您使用 php 通过 url 下载任何内容。关于上传?您想将文件保存在某处还是什么?

于 2012-08-29T17:13:56.487 回答