5

I have a website to show opensource movies and videos.

I have saved urls in mysql and linked both videos as well as the images to the content server.

But users are complaining of slow website as images are getting fetched from outside and most of time Internet Explorer is not even displaying the image.

I just learnt about cURL and would like to save images as well as videos to my own server and provide mirror to original website.

I got " curl -O ('') ; " syntax at many places to do the task but don't know how to use it inside my php script.

In short: I already have my form for url saving in mysql. I wish it to also save save file to a directory on my webserver and save file path to another column in mysql.

Any sort of help is welcome. Thanx in Advance

1 Answer 1

11
$local_file = "/tmp/filename.flv";//This is the file where we save the information
$remote_file = "http://www.test.com/filename.flv"; //Here is the file we are downloading


$ch = curl_init();
$fp = fopen ($local_file, 'w+');
$ch = curl_init($remote_file);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_exec($ch);
curl_close($ch);
fclose($fp);

I've decided to update this answer almost 7 years later.
For those who have copy() enabled for remote hosts, you can simply use:

copy("http://www.test.com/filename.flv", "/some/local/path/filename.flv");
7
  • Thanx for the code it works perfectly (there is small error of missing ; at end of second line for anyone else reading the post). Thank you for the code, it works perfectly on localhost. Going to test it on remote host now. Can you also please help me with a small piece of code to add in my form. So that file path and name are generated( /tmp/filename.flv ) and path info is saved to mysql. I can get $remote_file in form and save that info to mysql. But still have trouble generating the file name and saving path to mysql. Thanx once more for answer.
    – user899575
    Commented Aug 18, 2011 at 9:30
  • I've fixed the missing semicolon on the 2nd line. To be honest I'm not sure about what you need, what do you mean by "Can you also please help me with a small piece of code to add in my form" ? Commented Aug 18, 2011 at 14:06
  • I need a complete sample page with form to put url info and it adding path info to database and saving the file too. I know it is too much to ask for, but I look forward to your answer.
    – user899575
    Commented Aug 18, 2011 at 15:51
  • heavy files of 300mb+ not getting transferred
    – user899575
    Commented Aug 21, 2011 at 19:12
  • Try adding this code set_time_limit(0); at the top your php file. Commented Aug 21, 2011 at 20:17

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.