[PHP] Downloading a file with cURL

Status
Not open for further replies.

Whoo

The Wise One
2,480
2008
282
0
This thread is both a code snippet and a question.

Lets start with the code:
PHP:
$url = "http://downloadwebsite.com/file_you_are_going_to_download.rar";
$out = fopen("./".trim(basename($url)), 'wb+'); //Opens the temp file in the local directory of your server, same filename as the file you are downloading
$ch = curl_init(); //initialize curl 
curl_setopt($ch, CURLOPT_FILE, $out);  //Set it to download the content to the file created previously
curl_setopt($ch, CURLOPT_HEADER, 0); //Depending on the website you are downloading from there are headers that need to be sent
curl_setopt($ch, CURLOPT_URL, $url); //Set the url you want to start curling :P
curl_exec($ch); //Do the magic
curl_close($ch); //Close the magic
fclose($out); //Close the downloaded file properly

Now an issue:
On a server from a WJ member this worked perfectly, but now all of a sudden, instead of downloading the file to the server, it displays the contents of the file in the browser, any ideas why this is happening?

Thanks all :)
 
6 comments
So the file is to be downloaded to a member and not on the server. Correct?

If yes then maybe the member has changed the way his browser is handling the rar type file.
 
So the file is to be downloaded to a member and not on the server. Correct?

If yes then maybe the member has changed the way his browser is handling the rar type file.
No, the file is to be stored on the server:

PHP:
$out = fopen("./".trim(basename($url)), 'wb+'); //Opens the temp file in the local directory of your server, same filename as the file you are downloading
</span></span>
 
try this
PHP:
$url = "http://downloadwebsite.com/file_you_are_going_to_download.rar";
$out = fopen("./".trim(basename($url)), 'wb+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $out);
curl_exec($ch);
curl_close($ch);
fclose($out);
 
I have read that you must use CURLOPT_RETURNTRANSFER ,1 when doing it that way. Otherwise it sends it to the browser.

I never use it that way myself. I always use fopen to get the remote file , read and save it.
 
Hi,

Thanks for both suggestions, haven't been able to try it out yet because the person having the issue just switched server and hasn't reported back to me.

Thanks for the efforts thought.
 
Status
Not open for further replies.
Back
Top