PHP with password feature and download remote file save local specified directory modify enhanced version _php tips

Source: Internet
Author: User
Tags php code

The original author Bluestyle hints to improve the place there

The previous algorithm is to wait for the file download before the calculation,
Now this is a direct calculation of the size of the file when it gets
A fault tolerant statement is added
Added a judgment directory, no directory created automatically
The algorithm for calculating the size of the file is changed
The previous light calculation file size is 7 lines of code,
Now, this is just two lines.

Reprint please retain the original author copyright information, because the author is a government personnel, in order not to cause trouble, please retain this paragraph of text integrity
HTML code:
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Happy Flying Blog-remote file download </title>
<body >
<form method= "POST" >
<li>file: <input name= "url" size= "/>"
<input name= "Submit" type= "Submit"/></li>
<li>pass: <input name= "password" type= "password"/></li>
</form>

PHP Code:
Copy Code code as follows:

<?php
# Copyright 2010 Happy Flying
# http://www.klfy.org/for Beginners to learn the reference
Set_time_limit (0); No Limit 24 * 60 * 60
$password = ' admin '; Manage passwords
$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/'; Download the file to save the directory. Must end with slash
if (!is_dir ($destination _folder))//To determine whether the directory exists
mkdir ($destination _folder,0777); If none is created, and gives 777 permissions Windows ignores
$url = $_post[' url '];
$headers = Get_headers ($url, 1); Get File size
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> download time consuming: <font color= ' Blue ' > '. $runtime->spent (). ' </font> microseconds, file size <font Color= "Blue" > '. $headers ["Content-length"]. ' </font> bytes </li> ';
Echo ' <br/><li><font color= "Red" > Download success! '. $showtime =date ("y-m-d h:i:s"). ' </font></li> ';
}elseif (isset ($_post[' password ')) {
Echo ' <br/><li><font color= ' red ' > Password error! Please enter a new password!</font></li> ';
}
?>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.