Home > Backend Development > PHP Tutorial > PHP has password function and downloads remote files and saves them in the local specified directory. Modify the enhanced version_PHP tutorial

PHP has password function and downloads remote files and saves them in the local specified directory. Modify the enhanced version_PHP tutorial

WBOY
Release: 2016-07-21 15:37:45
Original
838 people have browsed it

PHP has password function and downloads remote files and saves them in the local specified directory. Modify the enhanced version_PHP tutorial
Original author BlueStyle suggested improvements:

The previous algorithm was to wait until the file was downloaded before calculating it.
Now this algorithm calculates the size directly when getting the file
Added fault tolerance The statement
adds a judgment directory. If there is no directory, it will be created automatically.
Change the algorithm for calculating the file size.
The previous one only required 7 lines of code to calculate the file size.
Now this one only needs two lines.

Please keep the copyright information of the original author when reprinting. Since the author is a government employee, in order not to cause trouble, please keep the integrity of this text
html code:

Copy code The code is as follows:





Happy Feiyang Blog - Remote File Download



  • File:

  • Pass:



  • PHP code:
    Copy code The code is as follows:

    # Copyright 2010 Happy Flying
    # http://www.klfy.org/ For novice learning reference
    set_time_limit ( 0); //No time limit 24 * 60 * 60
    $password = 'admin'; //Management 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 directory where downloaded files are saved. Must end with a slash
    if(!is_dir($destination_folder)) //Determine whether the directory exists
    mkdir($destination_folder,0777); //If not, create it and give 777 permissions. Windows ignores
    $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 '
  • Download time: '.$runtime->spent().' Microseconds, file size '.$headers["Content-Length"].' bytes
  • ';
    echo '
  • Download successful! '.$showtime=date("Y-m-d H:i:s").'
  • ';
    }elseif(isset($_POST['password'])){
    echo '
  • Wrong password! Please re-enter your password!
  • ';
    }
    ?>


    www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321903.htmlTechArticleOriginal author BlueStyle prompts for improvement: the previous algorithm waited for the file to be downloaded before calculating. Now this is obtained directly When calculating the file size, fault tolerance statements are added to increase judgment...
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template