ackerchez
ackerchez

Reputation: 1756

PHP Downloading with CURL

I have a problem that is driving me crazy.

I am trying to download a file from a remote server via a parameter that is being sent to me from an outside source and save it to a specific directory on my server.

Here is the code:

// value of this variable is something like http://api.com/20110720-202122_dceb89c3-a468-4de8-a37f-4f594bc5aab8.mp3

$recordingURL = $_REQUEST['RecordingURL'];

$filename = basename($recordingURL);

$path = '../mp3/TT/'.$filename;
if(!file_exists($path))
{    
if (!mkdir($path,'0755', true)) 
    {   
    errorLog('handlemp3.php','0','could not make recording directory'); 
    }
}

$fp = fopen($path, 'w');
$ch = curl_init($recordingURL);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);

At first I was seeing that the "TT" directory was not being created and I realized that the problem was that I did not set the recursive property in the mkdir to true. Once I did that, I saw the TT directory created on the server. The weird thing is that now I am not seeing the MP# created on the server.

Anyone have any idea why? I did a "$error = error_get_last()" no it and saw that I was getting an error like "Directive \'register_long_arrays\' is deprecated in PHP 5.3 and greater" but I have no idea what can be throwing that error.

Can someone help here?

Thanks!

Upvotes: 0

Views: 1569

Answers (3)

ackerchez
ackerchez

Reputation: 1756

Like an idiot, this is what I was doing wrong: 1) passed permission in as a string and not an integer 2) was including the file name in the mkdir so therefore the file name was within the folder but was set as a directory 3) did not include the file name in the fopen

Upvotes: 0

Alex Howansky
Alex Howansky

Reputation: 53636

Try this:

file_put_contents('/path/to/local/file', file_get_contents('http://site.com/remote/file'));

Upvotes: 1

cutsoy
cutsoy

Reputation: 10251

I'm not sure this is right:

curl_setopt($ch, CURLOPT_FILE, $fp);

Correct me if I'm wrong, but I think you are uploaded the contents of $fp instead of downloading the contents of $ch to $fp.

Remove the following lines:

$fp = fopen($path, 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
fclose($fp);

And put this at the end (so below curl_close):

file_put_contents($path, $data);

Upvotes: 0

Related Questions