doubao6464 2016-10-23 08: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 10: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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么