温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php怎么实现下载进度条

发布时间:2021-10-28 11:03:24 来源:亿速云 阅读:281 作者:iii 栏目:编程语言

本篇内容主要讲解“php怎么实现下载进度条”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“php怎么实现下载进度条”吧!

php实现下载进度条的方法:1、创建“download.php”文件,代码如“switch ($action) {case 'prepare-download'...}”;2、通过创建js代码显示进度条即可。

php怎么实现下载进度条

本文操作环境:Windows7系统、PHP7.1版、DELL G3电脑

php如何实现下载进度条?

PHP 远程文件下载的进度条实现

download.php

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

$action = @$_GET['action'];

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

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

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

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

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

        return json(compact('remote_url', 'tmp_path', 'file_size'));

        break;

    case 'start-download':

        // 这里检测下 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('发生错误:'.$e->getMessage());
        }

        return json(compact('tmp_path'));

        break;

    case 'get-file-size':

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

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

        break;

    default:
        # code...
        break;
}

js

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

    console.log("Prepared to download");

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

        file_size = json.file_size;

        $('#file-size').html(file_size);

        // 显示进度条

        console.log("started downloading");
        $.ajax({
            url: './download.php?action=start-download',
            type: 'POST',
            dataType: 'json'
        })
        .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() {

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

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

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

                    updateProgress(progress);

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

        }, 300);

    })
    .fail(showAjaxError);

}

到此,相信大家对“php怎么实现下载进度条”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI