Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using cURL to save external files to my Server

Tags:

php

curl

mysql

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

like image 405
user899575 Avatar asked Aug 17 '11 22:08

user899575


1 Answers

$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");
like image 59
Pedro Lobito Avatar answered Sep 30 '22 11:09

Pedro Lobito