开发者

PHP + CURL How to get file name

开发者 https://www.devze.com 2023-02-02 10:06 出处:网络
I\'m trying to download users profile picture from facebook in PHP using this function public static function downloadFile($url, $options = array())

I'm trying to download users profile picture from facebook in PHP using this function

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['fo开发者_JAVA技巧llowLocation']);
    curl_setopt($curl, CURLOPT_MAXREDIRS, $options['maxRedirs']);
    curl_exec($curl);

    curl_close($curl);
    fclose($fh);

    return $tempFileName;
}

The problem is it saves the file in the /tmp directory with a random name and without the extension. How can I get the original name of the file (I'm more interested in the original extension)

The important things here are:

  • The url actually redirects to image so i cant get it from original url
  • the final url does not have the file name in headers


If the URL redirects to an image, you should still be able to get the filename from the headers. Using a browser with dev tools like Chrome, you can look at the headers going back and forth from your original request to the resultant file, and the filename has to be in there somewhere. With that said, I was under the impression that Facebook rewrites all images to their own filenames for privacy reasons.

You can recreate the extension based on the content type of the response.

After running curl_exec(), try:

$content_type = curl_getinfo($curl,CURLINFO_CONTENT_TYPE);

This will return something like "image/jpg" for a JPEG, for example. You can then rename your temp file to have the proper extension.

If you can use PHP 5.3, a possibly cleaner way to do this would just be to call finfo_file() on the file after it is downloaded, which should read the MIME type, allowing you to select a proper extension without relying on header data. finfo_file() is also available in the fileinfo PECL extension.


If you're just looking for the image type (jpg/png/gif) and you have the GD extension installed you can use getimagesize, it returns the image type along with other details. It's more accurate than looking at the file extension.

image_type_to_mime_type might also help once you've used getimagesize.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号