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

php带密码功能并下载远程文件保存本地指定目录 修改加强版_PHP
原作者BlueStyle 提示 改进地方有

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

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




快乐飞扬博客 - 远程文件下载



  • File:

  • Pass:



  • 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 '
  • 下载耗时: '.$runtime->spent().' 微秒,文件大小 '.$headers["Content-Length"].' 字节
  • ';
    echo '
  • 下载成功! '.$showtime=date("Y-m-d H:i:s").'
  • ';
    }elseif(isset($_POST['password'])){
    echo '
  • 密码错误!请从新输入密码!
  • ';
    }
    ?>


    Statement
    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
    How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

    TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

    PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

    ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

    PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

    Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

    PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

    DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

    Simple Guide: Sending Email with PHP ScriptSimple Guide: Sending Email with PHP ScriptMay 12, 2025 am 12:02 AM

    PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

    PHP Performance: Identifying and Fixing BottlenecksPHP Performance: Identifying and Fixing BottlenecksMay 11, 2025 am 12:13 AM

    PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

    Dependency Injection for PHP: a quick summaryDependency Injection for PHP: a quick summaryMay 11, 2025 am 12:09 AM

    DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

    Increase PHP Performance: Caching Strategies & TechniquesIncrease PHP Performance: Caching Strategies & TechniquesMay 11, 2025 am 12:08 AM

    CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Article

    Hot Tools

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    Integrate Eclipse with SAP NetWeaver application server.

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment