7

I want to download a remote file and put it in my server directory with the same name the original has. I tried to use file_get_contents($url).

Problem is that the filename isn't included in $url, it is like: www.domain.com?download=1726. This URL give me, e.g.: myfile.exe, so I want to use file_put_contents('mydir/myfile.exe');.

How could I retrieve the filename? I tried get_headers() before downloading, but I only have file size, modification date and other information, the filename is missing.

Whisperity
  • 3,012
  • 1
  • 19
  • 36
Peter222
  • 154
  • 1
  • 16
  • Have a look at cURL it should cover what you need http://php.net/manual/en/book.curl.php – Waygood Aug 07 '12 at 09:17
  • Look at this question for cURL filename http://stackoverflow.com/questions/1750055/with-php-curl-get-filename-from-file-header – Waygood Aug 07 '12 at 09:21

3 Answers3

7

I solved it another way. I found that if there is no content-disposition in url headers, then filename exists in URL. So, this code works with any kind of URL's (no cURL needed):

$url = "http://www.example.com/download.php?id=123";
// $url = "http://www.example.com/myfile.exe?par1=xxx";
$content = get_headers($url,1);
$content = array_change_key_case($content, CASE_LOWER);

    // by header
if ($content['content-disposition']) {
    $tmp_name = explode('=', $content['content-disposition']);
    if ($tmp_name[1]) $realfilename = trim($tmp_name[1],'";\'');
} else  

// by URL Basename
{
    $stripped_url = preg_replace('/\\?.*/', '', $url);
    $realfilename = basename($stripped_url);

} 

It works! :)

Peter222
  • 154
  • 1
  • 16
5

Based on Peter222 's code i wrote a function to get the filename. You can use the $http_response_header variable:

function get_real_filename($headers,$url)
{
    foreach($headers as $header)
    {
        if (strpos(strtolower($header),'content-disposition') !== false)
        {
            $tmp_name = explode('=', $header);
            if ($tmp_name[1]) return trim($tmp_name[1],'";\'');
        }
    }

    $stripped_url = preg_replace('/\\?.*/', '', $url);
    return basename($stripped_url);
}

Usage: ($http_response_header will be filled by file_get_contents())

$url = 'http://example.com/test.zip';
$myfile = file_get_contents($url);
$filename = get_real_filename($http_response_header,$url)
Niksac
  • 770
  • 1
  • 10
  • 21
0

file_get_contents() over the HTTP wrapper does not directly download the file if the file has been pre-parsed by the webserver.

Take the following example: if you call file_get_contents() on a remove webpage (example.com/foobar.php), you won't be presented with the source code of foobar.php, but how the webserver of example.com parses the PHP file. So you will only be able to retrieve the generated HTML output.

If the filename is not present in the URL and there is no way that you can fetch it from anywhere, then you are in a dead end. Data can't be just summoned from the transcendental field of datum.

For alternative solutions, I can only suggest using the cURL library (it is used to handle queries from your server (as it were a client) to other servers with using URL, hence the name cient URL) or file sockets. Here is another question's answer on Stack Overflow which describes how to fetch filename using cURL.

Also, you might try getting in touch with the administrator/maintainer/webmaster team of domain.com, asking if they had a publicly available API to fetch filenames and other metadata.

Community
  • 1
  • 1
Whisperity
  • 3,012
  • 1
  • 19
  • 36
  • Thank You for fast reply. I will try cURL. But I still wonder, how browser knows exactly filename? When I put this url in browser, I get prompt save window with real filename. So, when I use before: ini_set('user_agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.16) Gecko/2009121601 Ubuntu/9.04 (jaunty) Firefox/3.0.16'); I thought I'm like a browser :) – Peter222 Aug 07 '12 at 09:40
  • [RFC 2183](http://www.ietf.org/rfc/rfc2183.txt) describes the `Content-Disposition:` header and that it is able to contain a `filename` directive. Browsers parse this part of the header and they fetch various metadata from it. Check the answer I linked as related in my answer, it describes a method how might one retrieve this header using PHP (and cURL). – Whisperity Aug 07 '12 at 09:43
  • I tested all of examples above. No success. I tried full this example class: http://stackoverflow.com/questions/6177661/how-do-download-a-file-with-curl but can't get original filename by cURL. Even if filename is included in URL. Could someone put working example based on any public file (jpg or something). Just download remote and save locally with the same name. I will be very grateful. Googling 2 days, no ideas :( – Peter222 Aug 08 '12 at 18:19