0

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 .

1 Answer 1

1

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');
Sign up to request clarification or add additional context in comments.

5 Comments

It didn't work for me , Note my public_html isn't writable , So that's why I want to download a file to any other writable directory
well simply change the path to the writable directory
what directory is writable under public_html?
/htdocs/mysite/images // The images directory is writable
I edited my post to reflect your writable directory and also added an example using your system() call

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.