搜索
首页后端开发php教程php带密码功能并下载远程文件保存本地指定目录 修改加强版_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 '
  • 密码错误!请从新输入密码!
  • ';
    }
    ?>


    声明
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    高流量网站的PHP性能调整高流量网站的PHP性能调整May 14, 2025 am 12:13 AM

    TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

    PHP中的依赖注入:初学者的代码示例PHP中的依赖注入:初学者的代码示例May 14, 2025 am 12:08 AM

    你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

    PHP性能:是否可以优化应用程序?PHP性能:是否可以优化应用程序?May 14, 2025 am 12:04 AM

    是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

    PHP性能优化:最终指南PHP性能优化:最终指南May 14, 2025 am 12:02 AM

    theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

    PHP依赖注入容器:快速启动PHP依赖注入容器:快速启动May 13, 2025 am 12:11 AM

    aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

    PHP中的依赖注入与服务定位器PHP中的依赖注入与服务定位器May 13, 2025 am 12:10 AM

    选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

    PHP性能优化策略。PHP性能优化策略。May 13, 2025 am 12:06 AM

    phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

    PHP电子邮件验证:确保正确发送电子邮件PHP电子邮件验证:确保正确发送电子邮件May 13, 2025 am 12:06 AM

    phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

    See all articles

    热AI工具

    Undresser.AI Undress

    Undresser.AI Undress

    人工智能驱动的应用程序,用于创建逼真的裸体照片

    AI Clothes Remover

    AI Clothes Remover

    用于从照片中去除衣服的在线人工智能工具。

    Undress AI Tool

    Undress AI Tool

    免费脱衣服图片

    Clothoff.io

    Clothoff.io

    AI脱衣机

    Video Face Swap

    Video Face Swap

    使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

    热门文章

    热工具

    Dreamweaver Mac版

    Dreamweaver Mac版

    视觉化网页开发工具

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    功能强大的PHP集成开发环境

    记事本++7.3.1

    记事本++7.3.1

    好用且免费的代码编辑器

    WebStorm Mac版

    WebStorm Mac版

    好用的JavaScript开发工具

    适用于 Eclipse 的 SAP NetWeaver 服务器适配器

    适用于 Eclipse 的 SAP NetWeaver 服务器适配器

    将Eclipse与SAP NetWeaver应用服务器集成。