开发者

Download Remote File to Server with PHP

开发者 https://www.devze.com 2023-03-13 04:13 出处:网络
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.

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 dir开发者_JAVA技巧ectory 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!


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.


$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


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'));


Use curl to download file from remote server like 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 );

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


file_put_contents expects a filename, not a directory name.


Split it into different 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 fetch is failing in file_get_contents, or the write is failing in file_put_contents, or the file you're downloading is too large and exceeds your PHP's default memory_limit.


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


With validations...

Validate if 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 file content (with 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

精彩评论

暂无评论...
验证码 换一张
取 消