3

I have a Curl Download File but The problems I have it's I need to output files with the filename where is output:

example:

$result = false;
     $path = "http://mywebsite.com/download.php?file=123";
        set_time_limit(0); //prevent timeout
        $ch2=curl_init();
        $file = fopen('upload/'.$name,'w+');
        curl_setopt($ch2, CURLOPT_URL, $url);
        curl_setopt($ch2, CURLOPT_FILE, $file); //auto write to file
        curl_setopt($ch2, CURLOPT_TIMEOUT, 5040);
        curl_setopt($ch2, CURLOPT_POST, 1);
        curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE); 
        curl_setopt($ch2, CURLOPT_SSLVERSION, 3); 
        curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true);
        curl_exec($ch2);    
        curl_close($ch2);
        fclose($file);

In my example, the I need the name of the download file is $name = (the file name)

like if I download file I can return custom_name_file.zip

but I need the writing file will the name of this custom_name_file

any idea?

Jean-philippe Emond
  • 1,619
  • 2
  • 17
  • 29
  • can't do it like this, since the name wouldn't be available until you've STARTED the download and gotten the http headers and extracted the Content-disposition stuff. You'd be better of writing it out to a temporary file, then renaming/moving that temp file afterwards. – Marc B May 02 '14 at 18:32
  • hu, I have no problem to output file in the tmp file and change name but, in this method, i can't really get the header at the same time? We'll need to be 2 curls to get the fileName? 0.o" – Jean-philippe Emond May 02 '14 at 18:49
  • curl has stupid header handling, but you CAN retrieve the headers using just one request: http://stackoverflow.com/questions/11659460/get-header-information-from-php-curl-post-request – Marc B May 02 '14 at 18:54

0 Answers0