Home  >  Article  >  Backend Development  >  How to implement download progress bar in php

How to implement download progress bar in php

藏色散人
藏色散人Original
2021-10-28 10:31:402883browse

php method to implement download progress bar: 1. Create the "download.php" file with code such as "switch ($action) {case 'prepare-download'...}"; 2. Create js The code displays a progress bar.

How to implement download progress bar in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

How to implement the download progress bar in php?

PHP remote file download progress bar implementation

download.php

<?php
// 当前文件:download.php

$action = @$_GET[&#39;action&#39;];

// 自己获取这些信息
$remote_url  = get_remote_file_url();
$file_size   = get_remote_file_size($remote_url);
$tmp_path    = get_tmp_path();

switch ($action) {
    case &#39;prepare-download&#39;:
        // 下载缓存文件夹
        $download_cache = __DIR__."/download_cache";

        if (!is_dir($download_cache)) {
            if (false === mkdir($download_cache)) {
                exit(&#39;创建下载缓存文件夹失败,请检查目录权限。&#39;);
            }
        }

        $tmp_path = $download_cache."/update_".time().".zip";

        save_tmp_path(); // 这里保存临时文件地址

        return json(compact(&#39;remote_url&#39;, &#39;tmp_path&#39;, &#39;file_size&#39;));

        break;

    case &#39;start-download&#39;:

        // 这里检测下 tmp_path 是否存在

        try {
            set_time_limit(0);

            touch($tmp_path);

            // 做些日志处理

            if ($fp = fopen($remote_url, "rb")) {

                if (!$download_fp = fopen($tmp_path, "wb")) {
                    exit;
                }

                while (!feof($fp)) {

                    if (!file_exists($tmp_path)) {
                        // 如果临时文件被删除就取消下载
                        fclose($download_fp);

                        exit;
                    }

                    fwrite($download_fp, fread($fp, 1024 * 8 ), 1024 * 8);
                }

                fclose($download_fp);
                fclose($fp);

            } else {
                exit;
            }

        } catch (Exception $e) {
            Storage::remove($tmp_path);

            exit(&#39;发生错误:&#39;.$e->getMessage());
        }

        return json(compact(&#39;tmp_path&#39;));

        break;

    case &#39;get-file-size&#39;:

        // 这里检测下 tmp_path 是否存在

        if (file_exists($tmp_path)) {
            // 返回 JSON 格式的响应
            return json([&#39;size&#39; => filesize($tmp_path)]);
        }

        break;

    default:
        # code...
        break;
}

js

// 咋触发这个函数我就不举例了
function downloadFile() {
    var file_size = 0;
    var progress  = 0;

    console.log("Prepared to download");

    $.ajax({
        url: &#39;./download.php?action=prepare-download&#39;,
        type: &#39;GET&#39;,
        dataType: &#39;json&#39;,
        beforeSend: function() {
            $(&#39;#update-button&#39;).html(&#39;<i class="fa fa-spinner fa-spin"></i> 正在准备&#39;).prop(&#39;disabled&#39;, &#39;disabled&#39;);
        },
    })
    .done(function(json) {
        console.log(json);

        file_size = json.file_size;

        $(&#39;#file-size&#39;).html(file_size);

        // 显示进度条

        console.log("started downloading");
        $.ajax({
            url: &#39;./download.php?action=start-download&#39;,
            type: &#39;POST&#39;,
            dataType: &#39;json&#39;
        })
        .done(function(json) {
            // set progress to 100 when got the response
            progress = 100;

            console.log("Downloading finished");
            console.log(json);
        })
        .fail(showAjaxError);

        var interval_id = window.setInterval(function() {

            $(&#39;#imported-progress&#39;).html(progress);
            $(&#39;.progress-bar&#39;).css(&#39;width&#39;, progress+&#39;%&#39;).attr(&#39;aria-valuenow&#39;, progress);

            if (progress == 100) {
                clearInterval(interval_id);

                // 到此远程文件下载完成,继续其他逻辑
            } else {
                $.ajax({
                    url: &#39;./download.php?action=get-file-size&#39;,
                    type: &#39;GET&#39;
                })
                .done(function(json) {
                    progress = (json.size / file_size * 100).toFixed(2);

                    updateProgress(progress);

                    console.log("Progress: "+progress);
                })
                .fail(showAjaxError);
            }

        }, 300);

    })
    .fail(showAjaxError);

}

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to implement download progress bar in php. For more information, please follow other related articles on the PHP Chinese website!

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