php文件下载实例

代码很简单就不多废话了,直接奉上:

php 自动下载远程服务器文件
<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 = ‘temp/’;
 
$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);
}
?>

php教程文件下载实例
这里主要是利用php
fopen函数来实现读取文件一个个传送到客户本地,有需要朋友可以参考一下。      

array(
      "method"=>"GET",
      "header"=>"",
      "timeout"=>$timeout)
    );
    $context = stream_context_create($opts);
    if(@copy($url, $file, $context)) {
      //$http_response_header
      return $file;
    } else {
      return false;
    }
  }
}
?>

<form method=”post”>        
<input name=”url” size=”20″ />        
<input name=”submit” type=”submit” />        
<!– <input type=”hidden” name=”MAX_FILE_SIZE” value=”2097152″
/>–>        
</form>        
<?php
    set_time_limit(24*60*60);
    if (!isset($_POST[‘submit’])) die ();
    $destination_folder = ‘./down/’;   //
文件夹保存下载文件。必须以斜杠结尾
    $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);
    }
?>

再来个远程下载文件到服务器

< ?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 = ‘temp/’;
$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); } ?>

以上所述就是本文的全部内容了,希望大家能够喜欢。

发表评论

电子邮件地址不会被公开。 必填项已用*标注