I've been looking all over the place for the last two days and trying everything and still can't get anything to work. I feel like this should be a relatively simple thing to do.
All I want to do is download a remote file from a URL to a directory on my server.
So, for example, if
$_url = http://www.freewarelovers.com/android/download/temp/1306495040_Number_Blink_1.1.1.apk
and $_dir = /www/downloads/
Then when all is said and done I want 1306495040_Number_Blink_1.1.1.apk
in /www/downloads/
I've tried the copy()
function, I've tried
file_put_contents("$_dir.$_file_name", file_get_contents($_url));
and get the following error:
file_get_contents(): failed to open stream: HTTP request failed!
Since PHP 5.1.0, file_put_contents() supports writing piece-by-piece by passing a stream-handle as the $data parameter:
No need to use Curl
file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));
$url = 'http://www.example.com/a-large-file.zip';
$path = '/path/to/a-large-file.zip';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
file_put_contents($path, $data);
it uses curl
$url is the file url
$path is where and the name to save the file
i hope it works
This should do it :
set_time_limit(0);
$url = 'http://www.freewarelovers.com/android/download/temp/1306495040_Number_Blink_1.1.1.apk';
$file = fopen(dirname(__FILE__) . '/downloads/a.apk', 'w+');
$curl = curl_init();
// Update as of PHP 5.4 array() can be written []
curl_setopt_array($curl, [
CURLOPT_URL => $url,
// CURLOPT_BINARYTRANSFER => 1, --- No effect from PHP 5.1.3
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FILE => $file,
CURLOPT_TIMEOUT => 50,
CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)'
]);
$response = curl_exec($curl);
if($response === false) {
// Update as of PHP 5.3 use of Namespaces Exception() becomes \Exception()
throw new \Exception('Curl error: ' . curl_error($curl));
}
$response; // Do something with the response.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With