doubao6464 2016-10-23 00:39 采纳率: 100%
浏览 120
已采纳

如何向从url下载文件到服务器的脚本添加进度条

I have searched for a script that download files from url to server and I came across this script. It works fine but I want to add some things to it like multi urls and progress bar. As I am new in php I know a little .

Here is the script in which I want to implement:

<html>
<form method="post">
<input name="url" size="50" />
<input name="submit" type="submit" />
</form>
<?php
    // maximum execution time in seconds
    set_time_limit (24 * 60 * 60);

    if (!isset($_POST['submit'])) die();

    // folder to save downloaded files to. must end with slash
    $destination_folder = 'downloads/';

    $url = $_POST['url'];
    $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);
    }
?>
</html> 

展开全部

  • 写回答

3条回答 默认 最新

  • duanba4254 2016-10-23 02:46
    关注

    For the multiple links you can tell the user to input all links seperated with commas. Then you could create an array with these links (using explode function) and loop over it until you download all files from the links. The code is below:

    <html>
    <form method="post">
    <input name="url" size="50" />
    <input name="submit" type="submit" />
    </form>
    <?php
    // maximum execution time in seconds
    set_time_limit (24 * 60 * 60);
    
    if (!isset($_POST['submit'])) die();
    
    // folder to save downloaded files to. must end with slash
    $destination_folder = 'downloads/';
    
    $urls = $_POST['url']; // I changed this variable's name to urls
    
    $links = explode(",",$urls); // links is now an array with the links
    
    foreach ($links as $url)
    {       
        $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);
        }
    }
    ?>
    

    Useful links:

    If you want to have another input box appear you would have to use javascript and it is a bit complicated. But if you want, I can show you that way too.

    For the progress bar, you will need javascript and css Bootstrap. I am trying to create a code that does what you want. When and if i manage to do so, I will upload it here, so you might want to come back later and check it out.

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 VAE代码如何画混淆矩阵
  • ¥15 求遗传算法GAMS代码
  • ¥15 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部