PHP Remote File download progress bar implementation

Source: Internet
Author: User
Tags compact

download.php

1<?PHP2 //Current file: download.php3 4 $action= @$_get[' Action '];5 6 //get this information yourself7 $remote _url=Get_remote_file_url ();8 $file _size= Get_remote_file_size ($remote _url);9 $tmp _path=Get_tmp_path ();Ten  One Switch($action) { A      Case' Prepare-download ': -         //Download Cache folder -         $download _cache= __dir__. " /download_cache "; the  -         if(!Is_dir($download _cache)) { -             if(false===mkdir($download _cache)) { -                 Exit(' Failed to create download cache folder, please check directory permissions. ‘); +             } -         } +  A         $tmp _path=$download _cache." /update_ ". Time().". Zip; at  -Save_tmp_path ();//Save temporary file address here -  -         returnJsonCompact(' Remote_url ', ' tmp_path ', ' file_size ')); -  -          Break; in  -      Case' Start-download ': to  +         //here detects if the Tmp_path exists -  the         Try { *             Set_time_limit(0); $ Panax Notoginseng             Touch($tmp _path); -  the             //do some log processing. +  A             if($fp=fopen($remote _url, "RB")) { the  +                 if(!$download _FP=fopen($tmp _path, "WB")) { -                     Exit; $                 } $  -                  while(!feof($fp)) { -  the                     if(!file_exists($tmp _path)) { -                         //Cancel Download If temporary file is deletedWuyi                         fclose($download _FP); the  -                         Exit; Wu                     } -  About                     fwrite($download _FP,fread($fp, 1024 * 8), 1024 * 8); $                 } -  -                 fclose($download _FP); -                 fclose($fp); A  +}Else { the                 Exit; -             } $  the}Catch(Exception $e) { theStorage::remove ($tmp _path); the  the             Exit(' Error occurred: '.$e-getMessage ()); -         } in  the         returnJsonCompact(' Tmp_path ')); the  About          Break; the  the      Case' Get-file-size ': the  +         //here detects if the Tmp_path exists -  the         if(file_exists($tmp _path)) {Bayi             //returns the JSON-formatted response the             returnJSON ([' size ' = =filesize($tmp _path)]); the         } -  -          Break; the  the     default: the         #code ... the          Break; -}

Js

1 //I'm not going to give you an example of how to trigger this function2 functionDownloadFile () {3     varFile_size = 0;4     varProgress = 0;5 6Console.log ("Prepared to download");7 8 $.ajax ({9URL: './download.php?action=prepare-download ',TenType: ' GET ', OneDataType: ' JSON ', ABeforesend:function() { -$ (' #update-button '). html (' <i class= ' fa fa-spinner fa-spin ' ></i> preparing '). Prop (' disabled ', ' disabled '); -         }, the     }) -. Done (function(JSON) { - Console.log (JSON); -  +File_size =json.file_size; -  +$ (' #file-size '). HTML (file_size); A  at         //Show progress bar -  -Console.log ("Started downloading"); - $.ajax ({ -URL: './download.php?action=start-download ', -Type: ' POST ', inDataType: ' JSON ' -         }) to. Done (function(JSON) { +             //set progress to got the response -progress = 100; the  *Console.log ("Downloading finished"); $ Console.log (JSON);Panax Notoginseng         }) - . Fail (showajaxerror); the  +         varinterval_id = Window.setinterval (function() { A  the$ (' #imported-progress '). HTML (progress); +$ ('. Progress-bar '). CSS (' width ', progress+ '% '). attr (' Aria-valuenow '), progress); -  $             if(Progress = = 100) { $ clearinterval (interval_id); -                -                 //until this remote file download is complete, continue with other logic the}Else { - $.ajax ({WuyiURL: './download.php?action=get-file-size ', theType: ' GET ' -                 }) Wu. Done (function(JSON) { -Progress = (Json.size/file_size *). toFixed (2); About                    $ UpdateProgress (progress); -  -Console.log ("Progress:" +progress); -                 }) A . Fail (showajaxerror); +             } the  -}, 300); $  the     }) the . Fail (showajaxerror); the  the}

PHP Remote File download progress bar implementation

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.