Bestand downloaden naar server vanaf URL

Nou, deze lijkt vrij eenvoudig, en dat is het ook. Het enige wat u hoeft te doen om een ​​bestand naar uw server te downloaden is:

file_put_contents("Tmpfile.zip", file_get_contents("http://someurl/file.zip"));

Er is alleen één probleem. Wat als je een groot bestand hebt, zoals 100 MB. Dan heeft u onvoldoende geheugen en kunt u het bestand niet downloaden.

Wat ik wil is een manier om het bestand naar de schijf te schrijven terwijl ik het download. Op die manier kan ik grotere bestanden downloaden, zonder geheugenproblemen te krijgen.


Antwoord 1, autoriteit 100%

Sinds PHP 5.1.0 ondersteunt file_put_contents()het schrijven per stuk door een stream-handle door te geven als de parameter $data:

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

Uit de handleiding:

Als data[dat is het tweede argument] een streambron is, wordt de resterende buffer van die stream gekopieerd naar het opgegeven bestand. Dit is vergelijkbaar met het gebruik van
stream_copy_to_stream().

(Bedankt Hakre.)


Antwoord 2, autoriteit 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);
    }
}

Antwoord 3, autoriteit 13%

Probeer cURL te gebruiken

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

Ik weet het niet zeker, maar ik geloof dat het met de optie CURLOPT_FILEschrijft terwijl het de gegevens ophaalt, dwz. niet gebufferd.


Antwoord 4, autoriteit 4%

  1. Maak een map met de naam “downloads” op de doelserver
  2. Sla [deze code] op in het .php-bestand en voer het uit op de doelserver

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> 

Antwoord 5, autoriteit 3%

set_time_limit(0); 
$file = file_get_contents('path of your file');
file_put_contents('file.ext', $file);

Antwoord 6, autoriteit 2%

Er zijn 3 manieren:

  1. file_get_contents en file_put_contents
  2. KRUL
  3. fopen

Je kunt van hiervoorbeelden vinden.


Antwoord 7, autoriteit 2%

Gebruik een eenvoudige methode in php copy()

copy($source_url, $local_path_with_file_name);

Opmerking: als het doelbestand al bestaat, wordt het overschreven

PHP copy()-functie

Opmerking: u moet toestemming 777 instellen voor de doelmap.
Gebruik deze methode wanneer u downloadt naar uw lokale computer.

Speciale opmerking: 777 is een machtiging in een op Unix gebaseerd systeem met volledige lees-/schrijf-/uitvoermachtiging voor de eigenaar, groep en iedereen.Over het algemeen geven we deze machtiging aan middelen die niet veel nodig zijn om worden verborgen voor het publiek op een webserver. Voorbeeld: map afbeeldingen.


Antwoord 8

Ik gebruik dit om een ​​bestand te downloaden

function cURLcheckBasicFunctions()
{
  if( !function_exists("curl_init") &&
      !function_exists("curl_setopt") &&
      !function_exists("curl_exec") &&
      !function_exists("curl_close") ) return false;
  else return true;
}
/*
 * Returns string status information.
 * Can be changed to int or bool return types.
 */
function cURLdownload($url, $file)
{
  if( !cURLcheckBasicFunctions() ) return "UNAVAILABLE: cURL Basic Functions";
  $ch = curl_init();
  if($ch)
  {
    $fp = fopen($file, "w");
    if($fp)
    {
      if( !curl_setopt($ch, CURLOPT_URL, $url) )
      {
        fclose($fp); // to match fopen()
        curl_close($ch); // to match curl_init()
        return "FAIL: curl_setopt(CURLOPT_URL)";
      }
      if ((!ini_get('open_basedir') && !ini_get('safe_mode')) || $redirects < 1) {
        curl_setopt($ch, CURLOPT_USERAGENT, '"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($ch, CURLOPT_REFERER, 'http://domain.com/');
        if( !curl_setopt($ch, CURLOPT_HEADER, $curlopt_header)) return "FAIL: curl_setopt(CURLOPT_HEADER)";
        if( !curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $redirects > 0)) return "FAIL: curl_setopt(CURLOPT_FOLLOWLOCATION)";
        if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
        if( !curl_setopt($ch, CURLOPT_MAXREDIRS, $redirects) ) return "FAIL: curl_setopt(CURLOPT_MAXREDIRS)";
        return curl_exec($ch);
    } else {
        curl_setopt($ch, CURLOPT_USERAGENT, '"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($ch, CURLOPT_REFERER, 'http://domain.com/');
        if( !curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false)) return "FAIL: curl_setopt(CURLOPT_FOLLOWLOCATION)";
        if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
        if( !curl_setopt($ch, CURLOPT_HEADER, true)) return "FAIL: curl_setopt(CURLOPT_HEADER)";
        if( !curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)) return "FAIL: curl_setopt(CURLOPT_RETURNTRANSFER)";
        if( !curl_setopt($ch, CURLOPT_FORBID_REUSE, false)) return "FAIL: curl_setopt(CURLOPT_FORBID_REUSE)";
        curl_setopt($ch, CURLOPT_USERAGENT, '"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');
    }
      // if( !curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true) ) return "FAIL: curl_setopt(CURLOPT_FOLLOWLOCATION)";
      // if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
      // if( !curl_setopt($ch, CURLOPT_HEADER, 0) ) return "FAIL: curl_setopt(CURLOPT_HEADER)";
      if( !curl_exec($ch) ) return "FAIL: curl_exec()";
      curl_close($ch);
      fclose($fp);
      return "SUCCESS: $file [$url]";
    }
    else return "FAIL: fopen()";
  }
  else return "FAIL: curl_init()";
}

Other episodes