Download Remote File to Server with PHP

2019-01-18 06:56发布

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!

7条回答
欢心
2楼-- · 2019-01-18 07:24

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

查看更多
SAY GOODBYE
3楼-- · 2019-01-18 07:30

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.
查看更多
女痞
4楼-- · 2019-01-18 07:30

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

查看更多
贪生不怕死
5楼-- · 2019-01-18 07:38

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.

查看更多
可以哭但决不认输i
6楼-- · 2019-01-18 07:42

file_put_contents expects a filename, not a directory name.

查看更多
forever°为你锁心
7楼-- · 2019-01-18 07:43

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'));
查看更多
登录 后发表回答