php带密码功能并下载远程文件保存本地指定目录 修改加强版

yipeiwu_com5年前PHP代码库

原作者BlueStyle 提示 改进地方有

以前的算法是等文件下载完才计算,
现在这个直接在在获取文件时候就计算大小
加了容错语句
增加了判断目录,没有目录自动创建
把计算文件大小的算法换了个
以前的那个光计算文件大小就7行代码,
现在这个只要两行

转载请保留原作者版权信息,由于作者是政府人员,为不惹麻烦,请保留此段文字完整性
html代码:
复制代码 代码如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>快乐飞扬博客 - 远程文件下载</title>
</head>
<body >
<form method="post">
<li>File: <input name="url" size="40" />
<input name="submit" type="submit" /></li>
<li>Pass: <input name="password" type="password" /></li>
</form>

PHP代码:
复制代码 代码如下:

<?php
# Copyright 2010 快乐飞扬
# http://www.klfy.org/ 供新手学习参考
set_time_limit (0); //不限时 24 * 60 * 60
$password = 'admin'; //管理密码
$pass = $_POST['password'];
if ($pass == $password) {
class runtime {
var $StartTime = 0;
var $StopTime = 0;
function get_microtime(){list($usec, $sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);}
function start() {$this->StartTime = $this->get_microtime();}
function stop() {$this->StopTime = $this->get_microtime();}
function spent() { return round(($this->StopTime - $this->StartTime) * 1000, 1);}
}
$runtime= new runtime;
$runtime->start();
if (!isset($_POST['submit'])) die();
$destination_folder = './Download/'; // 下载的文件保存目录。必须以斜杠结尾
if(!is_dir($destination_folder)) //判断目录是否存在
mkdir($destination_folder,0777); //若无则创建,并给与777权限 windows忽略
$url = $_POST['url'];
$headers = get_headers($url, 1); //得到文件大小
if ((!array_key_exists("Content-Length", $headers))) {$filesize=0; }
$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);}
$runtime->stop();
echo '<br /><li>下载耗时:<font color="blue"> '.$runtime->spent().' </font>微秒,文件大小<font color="blue"> '.$headers["Content-Length"].' </font>字节</li>';
echo '<br /><li><font color="red">下载成功! '.$showtime=date("Y-m-d H:i:s").'</font></li>';
}elseif(isset($_POST['password'])){
echo '<br /><li><font color="red">密码错误!请从新输入密码!</font></li>';
}
?>
</body>
</html>

相关文章

php curl 伪造IP来源的实例代码

curl发出请求的文件fake_ip.php: 代码 复制代码 代码如下: <?php $ch = curl_init(); $url = "http://localhost/ta...

php自动加载的两种实现方法

php自动载方法有两种. 第一种方案用__autoload,这个函数较简单,也较弱. 但有一问题没有解决, 就是在include前判断文件是否存在的问题. 复制代码 代码如下: set_...

php中heredoc与nowdoc介绍

Heredoc技术,在正规的PHP文档中和技术书籍中一般没有详细讲述,只是提到了这是一种Perl风格的字符串输出技术。但是现在的一些论坛程序,和部分文章系统,都巧妙的使用heredoc技...

php set_time_limit()函数的使用详解

语法 : void set_time_limit (int seconds)说明 : 设定一个程式所允许执行的秒数,如果到达限制的时间,程式将会传回错误。它预设的限制时间是30秒,max...

有关PHP中MVC的开发经验分享

一、入口 入口文件可以是单文件也可以是多文件,我现在用的基本属多文件,但是入口文件内容基本都是一样,为以后的修改其它的入口方式做基础, 复制代码 代码如下: <?php requi...