Upload remote file to server using PHP

I look everywhere for the past two days and try my best, but still can't get it to work. I feel this should be a relatively simple task.

All I want to do is upload 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 everything is said and done, I want 1306495040_Number_Blink_1.1.1.apk in /www/downloads/

I tried the copy() function, I 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!

+7
source share
8 answers

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. 
+15
source
 $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); 

he uses curl

$ url is the file url

$ path is where and the name to save the file

Hope it works.

+14
source

Use curl to download the file from a remote server, as shown below.

 $url = "http://path/toserver/filename"; $destination = "uploads/filename"; $fp = fopen ($destination, 'w+'); $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_BINARYTRANSFER, true ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false ); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 ); curl_setopt( $ch, CURLOPT_FILE, $fp ); curl_exec( $ch ); curl_close( $ch ); fclose( $fp ); 

link http://www.tricksofit.com/2014/04/download-file-from-remote-server-in-php

+5
source

file_put_contents expects a file name, not a directory name.

+4
source

Since PHP 5.1.0, file_put_contents () supports writing in parts by passing the stream descriptor as the $ data parameter:
No need to use curl

 file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r')); 
+3
source

Divide it into several stages:

 $raw = file_get_contents($_url); ... check if $raw has anything useful in it file_put_contents($_dir, $raw); ... check if the file showed up 

Either the extraction occurs in the file_get_contents file, or in the write_put_contents file, or the file you are loading is too large and exceeds your PHP default memory_limit.

+1
source

You may not have fopen wrappers, so file_get_contents may not work. Try using curl or a library like Snoopy .

0
source

With checks ...

Check if the file exists first:

 function doesUrlExists($url) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_NOBODY, true); curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if($code == 200){ $status = true; }else{ $status = false; } curl_close($ch); return $status; } 

And then put the contents of the file (with the laravel storage class):

  if(!doesUrlExists($url_file)) { die('The remote file is not accessible. Please check the URL.'); } Storage::disk('local') ->put($file_destintation, fopen($url_file, 'r')); 
0
source

Source: https://habr.com/ru/post/890532/


All Articles