简体   繁体   中英

Copy file from remote server using PHP over HTTP

I want to copy a file using PHP over http from a link in this format

http://myserver.com/?id=1234

if I open the link, the download of the file starts ...

So I assume that server redirects to a .mp3 file to start the download.

So how to copy/download the file from the remote server to to my server (localhost)?

Just to gove an example of what Victor is tlking about with cURL:

$options = array(
  CURLOPT_FILE => '/local/path/for/file.mp3',
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_URL => 'http://myserver.com/?id=1234',

);

$ch = curl_init();
curl_setopt_array($ch, $options);
curl_exec($ch);

I'm assuming here that the remote server sends the complete file over HTTP. You could use a library such as curl to send an HTTP request and store the received data as a file (using CURLOPT_FILE ).

If your local PHP server is correctly configured, you can also use copy to copy from a remote URL to a local path.

Try using a notification callback (read here for mor informations http://www.php.net/manual/function.stream-notification-callback.php )

eg you could to this if you like to copy:

function stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max)
{
  if($notification_code == STREAM_NOTIFY_PROGRESS)
  {
    // save $bytes_transferred and $bytes_max to file or database
  }
}


$ctx = stream_context_create();
stream_context_set_params($ctx, array("notification" => "stream_notification_callback"));
copy($remote_url,$Local_target,$ctx); 

Another PHP file could read the saved $bytes_transferred and $bytes_max and show a nice progress bar.

$handle = fopen("http://www.example.com/", "rb"); $contents = ''; while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle);

from

http://php.net/manual/en/function.fread.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM