Download File to server from URL

后端 未结 12 1214
旧巷少年郎
旧巷少年郎 2020-11-22 04:05

Well, this one seems quite simple, and it is. All you have to do to download a file to your server is:

file_put_contents(\"Tmpfile.zip\", file_get_contents(\         


        
相关标签:
12条回答
  • 2020-11-22 04:16

    Try using cURL

    set_time_limit(0); // unlimited max execution time
    $options = array(
      CURLOPT_FILE    => '/path/to/download/the/file/to.zip',
      CURLOPT_TIMEOUT =>  28800, // set this to 8 hours so we dont timeout on big files
      CURLOPT_URL     => 'http://remoteserver.com/path/to/big/file.zip',
    );
    
    $ch = curl_init();
    curl_setopt_array($ch, $options);
    curl_exec($ch);
    curl_close($ch);
    

    I'm not sure but I believe with the CURLOPT_FILE option it writes as it pulls the data, ie. not buffered.

    0 讨论(0)
  • 2020-11-22 04:17

    Simple solution:

    <?php 
    exec('wget http://someurl/file.zip');
    
    0 讨论(0)
  • 2020-11-22 04:21

    Since PHP 5.1.0, file_put_contents() supports writing piece-by-piece by passing a stream-handle as the $data parameter:

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

    From the manual:

    If data [that is the second argument] is a stream resource, the remaining buffer of that stream will be copied to the specified file. This is similar with using stream_copy_to_stream().

    (Thanks Hakre.)

    0 讨论(0)
  • 2020-11-22 04:23

    A PHP 4 & 5 Solution:

    readfile() will not present any memory issues, even when sending large files, on its own. A URL can be used as a filename with this function if the fopen wrappers have been enabled.

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

    0 讨论(0)
  • 2020-11-22 04:24
    1. Create a folder called "downloads" in destination server
    2. Save [this code] into .php file and run in destination server

    Downloader :

    <html>
    <form method="post">
    <input name="url" size="50" />
    <input name="submit" type="submit" />
    </form>
    <?php
        // maximum execution time in seconds
        set_time_limit (24 * 60 * 60);
    
        if (!isset($_POST['submit'])) die();
    
        // folder to save downloaded files to. must end with slash
        $destination_folder = 'downloads/';
    
        $url = $_POST['url'];
        $newfname = $destination_folder . basename($url);
    
        $file = fopen ($url, "rb");
        if ($file) {
          $newf = fopen ($newfname, "wb");
    
          if ($newf)
          while(!feof($file)) {
            fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
          }
        }
    
        if ($file) {
          fclose($file);
        }
    
        if ($newf) {
          fclose($newf);
        }
    ?>
    </html> 
    
    0 讨论(0)
  • 2020-11-22 04:25
    private function downloadFile($url, $path)
    {
        $newfname = $path;
        $file = fopen ($url, 'rb');
        if ($file) {
            $newf = fopen ($newfname, 'wb');
            if ($newf) {
                while(!feof($file)) {
                    fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
                }
            }
        }
        if ($file) {
            fclose($file);
        }
        if ($newf) {
            fclose($newf);
        }
    }
    
    0 讨论(0)
提交回复
热议问题