PHP with password function and download the remote file to save the local specified directory modification enhanced version

  • 2020-03-31 20:43:46
  • OfStack

< img border = 0 SRC = "http://files.jb51.net/upload/201005/20100516231004796.gif" border = 0 >
Original author BlueStyle hints for improvements

The old algorithm waited until the file was downloaded to calculate,
Now this is directly calculating the size when you get the file
Added fault tolerant statements
Added judge directory, no directory automatically created
Change the algorithm to calculate the file size
The previous one was just seven lines of code to calculate the file size,
Now this is just two rows

Reprint please retain the original author's copyright information, because the author is a government personnel, in order not to cause trouble, please retain the text integrity
HTML code:
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title> Happy flying blog  -  Remote file download </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> 

The PHP code:
 
<?php 
# Copyright 2010  Happy flying  
# http://www.klfy.org/  For beginners to learn reference  
set_time_limit (0); //24 times 60 times 60
$password = 'admin'; //Administrative password
$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/'; //The downloaded files are saved in the directory. You must end with a slash
if(!is_dir($destination_folder)) //Determine if the directory exists
mkdir($destination_folder,0777); //If not, create, and give 777 rights Windows ignore
$url = $_POST['url']; 
$headers = get_headers($url, 1); //Get the 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 :<font color="blue"> '.$runtime->spent().' </font> microseconds , The file size <font color="blue"> '.$headers["Content-Length"].' </font> byte </li>'; 
echo '<br /><li><font color="red"> Download successful!  '.$showtime=date("Y-m-d H:i:s").'</font></li>'; 
}elseif(isset($_POST['password'])){ 
echo '<br /><li><font color="red"> Password error! Please reenter your password !</font></li>'; 
} 
?> 
</body> 
</html> 

Related articles: