【发布时间】:2010-12-31 07:13:43
【问题描述】:
我正在尝试使用此功能在 PHP 中从 facebook 下载用户个人资料图片
public static function downloadFile($url, $options = array())
{
if (!is_array($options))
$options = array();
$options = array_merge(array(
'connectionTimeout' => 5, // seconds
'timeout' => 10, // seconds
'sslVerifyPeer' => false,
'followLocation' => true, // if true, limit recursive redirection by
'maxRedirs' => 2, // setting value for "maxRedirs"
), $options);
// create a temporary file (we are assuming that we can write to the system's temporary directory)
$tempFileName = tempnam(sys_get_temp_dir(), '');
$fh = fopen($tempFileName, 'w');
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_FILE, $fh);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $options['connectionTimeout']);
curl_setopt($curl, CURLOPT_TIMEOUT, $options['timeout']);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $options['sslVerifyPeer']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $options['followLocation']);
curl_setopt($curl, CURLOPT_MAXREDIRS, $options['maxRedirs']);
curl_exec($curl);
curl_close($curl);
fclose($fh);
return $tempFileName;
}
问题在于它将文件保存在 /tmp 目录中,名称随机且没有扩展名。如何获取文件的原始名称(我对原始扩展名更感兴趣)
这里重要的事情是:
- 网址实际上重定向到图片,所以我无法从原始网址获取它
- 最终的 url 在 headers 中没有文件名
【问题讨论】:
标签: php facebook curl download