Ahmed Magdy
Ahmed Magdy

Reputation: 25

cURL and PHP issue

So far I want a curl command to download a file into a specific directory , So I have done this

system('curl -o hi.txt www.site.com/hi.html');

It doesn't work because my directory isn't writable , I need to find a way to set curl to download that file to a writable directory of mine .

Upvotes: 0

Views: 43

Answers (1)

Pastor Bones
Pastor Bones

Reputation: 7351

Instead of curl you could use file_get_contents and file_put_contents

$file = file_get_contents('http://www.site.com/hi.html');
file_put_contents('/htdocs/mysite/images/hi.txt', $file);

This method will work even if the curl module isn't installed. To do this using cURL (which will allow more control over the actual http request):

// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.site.com/hi.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
$out = curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

// Save the file
$fp = fopen('/htdocs/mysite/images/hi.txt', 'w');
fwrite($fp, $out);
fclose($fp);

EDIT

system('curl -o /htdocs/mysite/images/hi.txt www.site.com/hi.html');

Upvotes: 1

Related Questions