Kyle Domingo
Kyle Domingo

Reputation: 531

PHP curl fails to download data when outputting to a file but ok when printing to stdout

This has been bugging me for literally hours already.

I can't seem to figure out why PHP cURL won't download data to a file. (CURLOPT_FILE is set to a local file.) I am not getting any data. I periodically check the file size of the destination file and it is always zero. To give you a background, I am downloading a 90kb jpeg file (for testing purposes).

This is working on my local computer (XP) but not in the website I am working on (Windows Server 2003).

I did several tests which made the scenario even weirder.

  1. I disabled CURLOPT_FILE to print the data returned by curl into standard output, and the binary data printed.

  2. Having experienced blocked websites before (since the server implements access control), I tried accessing the file from internet explorer and i was able to see it.

  3. Having experienced blocked downloads before, I tried downloading the file from internet explorer and it was downloaded.

  4. The file is created by fopen('', 'w') but the size remains 0. Despite this successful file creation, I thought maybe PHP has a problem with filesystem write privileges, I set the exe to be run even by non-admin users. Still no download.

Has this ever occured to anybody?

Any pointers will be appreciated. I am really stuck.

Thank you.

Here's the curl options I set:

$connection = curl_init($src);

// If these are not set, curl_exec outputs data.
// If these are set, curl_exec does not send any data to the file
// pointed to by $file_handler. $file_handler is not null
// because it is opened as write (non-existing file is created)

curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $connection, CURLOPT_FILE, $file_handler );

PS: I'm doing these tests using the command line and not the browser.

Upvotes: 0

Views: 2063

Answers (3)

Joe P.
Joe P.

Reputation: 338

You may need to figure out what user runs your php, if the user running the php script (the one that calls php ) is not authorized to write to the directory of the file, or to the /path/to/file , you may need to adjust your file permissions.

Upvotes: 0

Melvin Loos
Melvin Loos

Reputation: 31

I don't think you have to set CURLOPT_RETURNTRANSFER and if you are running from the command line be sure to run the php with admin rights. Not sure how it works in windows but in linux I always sudo every command line script I run.

Also if php safe mode is on be sure to also give the the directory the same (UID) owner as the php file. Hmh but since you can create the file (with 0 filesize) it might have nothing to do with rights... could you check the *open_basedir* php setting on your server? If it is set cUrl is not allowed to use file protocol... did you check the log files from your server? maybe there is an error.

Upvotes: 1

Joe P.
Joe P.

Reputation: 338

you might not have permissions to write to the file.

Upvotes: 1

Related Questions