PHP 获取文件上传进度

获取文件上传进度的方法很多,该文介绍的是使用session.upload_progress,基于PHP 5.4以上版本的方法。

【1】文件php.ini 配置

根据实际情况进行设置

session.upload_progress.enabled[=1] : 是否启用上传进度报告(默认开启)

session.upload_progress.cleanup[=1] : 是否在上传完成后及时删除进度数据(默认开启, 推荐开启).

session.upload_progress.prefix[=upload_progress_] : 进度数据将存储在_SESSION[session.upload_progress.prefix . _POST[session.upload_progress.name]]

session.upload_progress.name[=PHP_SESSION_UPLOAD_PROGRESS] : 如果_POST[session.upload_progress.name]没有被设置, 则不会报告进度.

session.upload_progress.freq[=1%] : 更新进度的频率(已经处理的字节数), 也支持百分比表示’%’.

session.upload_progress.min_freq[=1.0] : 更新进度的时间间隔(秒级)

;允许客户端单个POST请求发送的最大数据 M

post_max_size=2048M

;接受所有数据到开始执行脚本的最大时间 S

max_input_time=60

;脚本的最大执行时间 S

max_execution_time=0

;是否开启文件上传功能

file_uploads = On

;文件上传的临时目录

upload_tmp_dir="C:\xampp\tmp"

;允许单个请求上传的最大文件大小 M

upload_max_filesize = 2048M

;允许单个POST请求同时上传的最大文件数量

max_file_uploads = 20

【2】 _FILES变量简介

$_FILES["file"]["name"] - 被上传文件的名称
$_FILES["file"]["type"] - 被上传文件的类型
$_FILES["file"]["size"] - 被上传文件的大小,以字节计
$_FILES["file"]["tmp_name"] - 存储在服务器的文件的临时副本的名称
$_FILES["file"]["error"] - 由文件上传导致的错误代码

【3】代码

文件上传表单

设置session.upload_progress.name key的值

<?php
if (version_compare(phpversion(), '5.4.0', '<'))
    exit('ERROR: Your PHP version is ' . phpversion() . ' but this script requires PHP 5.4.0 or higher.');

if (!intval(ini_get('session.upload_progress.enabled')))
    exit('session.upload_progress.enabled is not enabled, please activate it in your PHP config file to use this script.');

require_once ("upload.class.php");
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form id="upload-form" action="upload.php" method="POST" enctype="multipart/form-data">
 <!--设置session.upload_progress.name Key的值-->
 <input type="hidden"
  name="<?php echo ini_get("session.upload_progress.name"); ?>" value="<?php echo CUpload::UPLOAD_PROGRESS_PREFIX; ?>" />
 <table>
 <tr>
 <td><input name="file1[]" type="file" align="right"/></td>
 <td><input type="submit" name="Submit" value="上传"/></td>
 </tr>
 </table>
</form>
</body>
</html>

文件上传处理类

<?php
class CUpload
{
    const   UPLOAD_PROGRESS_PREFIX = 'progress_bar';
    private $_sMsg,  $_sUploadDir,  $_sProgressKey;

    // The short array syntax (only for PHP 5.4.0 and higher)
    private $_aErrFile = [
         UPLOAD_ERR_OK         => 'There is no error, the file uploaded with success.',
         UPLOAD_ERR_INI_SIZE   => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
         UPLOAD_ERR_FORM_SIZE  => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
         UPLOAD_ERR_PARTIAL    => 'The uploaded file was only partially uploaded.',
         UPLOAD_ERR_NO_FILE    => 'No file was uploaded.',
         UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.',
         UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
         UPLOAD_ERR_EXTENSION  => 'A PHP extension stopped the file upload.'
    ];

    public function __construct()
    {
        // Session initialization
        if ('' === session_id()) session_start();

        $this->_sUploadDir = 'uploads/';
        $this->_sProgressKey = strtolower(ini_get('session.upload_progress.prefix') . static::UPLOAD_PROGRESS_PREFIX);
    }   

    public function __destruct()
    {
        if ('' !== session_id())
        {
            $_SESSION = array();
            session_destroy();
        }
    }

    /**
     * @return object this
     */
    public function addFile()
    {
        if(!empty($_FILES))
        {
            $this->_sMsg = '';

            foreach($_FILES as $sKey => $aFiles)
            {
                for($i = 0, $iNumFiles = count($aFiles['tmp_name']); $i < $iNumFiles; $i++)
                {
                    /**
                     * 获取上传文件的参数
                     */
                    $iErrCode   = $aFiles['error'][$i];
                    $iSize      = $aFiles['size'][$i];
                    $sFileName  = $aFiles['name'][$i];
                    $sTmpFile   = $aFiles['tmp_name'][$i];
                    $sFileDest  = $this->_sUploadDir . $sFileName;
                    $sTypeFile  = $aFiles['type'][$i];

                    /**
                     * 检测文件类型
                     */
                    //$bIsImgExt = (strtolower(substr(strrchr($sFileName, '.'), 1))); // Get the file extension
                    //if(($bIsImgExt == 'jpeg' || $bIsImgExt == 'jpg' || $bIsImgExt == 'png' || $bIsImgExt == 'gif') && (strstr($sTypeFile, '/', true) === 'image'))
                    {
                        if($iErrCode == UPLOAD_ERR_OK)
                        {
                            move_uploaded_file($sTmpFile, $sFileDest);

                            $this->_sMsg .= '<p style="color:green; font-weight:bold; text-align:center">Successful "' . $sFileName . '" file upload!</p>';
                            $this->_sMsg .= '<p style="text-align:center">Image type: ' . str_replace('image/', '', $sTypeFile) . '<br />';
                            $this->_sMsg .= 'Size: ' . round($iSize / 1024) . ' KB<br />';
                            $this->_sMsg .= '<a href="' . $sFileDest . '" title="Click here to see the original file" target="_blank"><img src="' . $sFileDest . '" alt="' . $sFileName  . '" width="300" height="250" style="border:1.5px solid #ccc; border-radius:5px" /></a></p>';
                        }
                        else
                        {
                            $this->_sMsg .= '<p style="color:red; font-weight:bold; text-align:center">Error while downloading the file "' . $sFileName . '"<br />';
                            $this->_sMsg .= 'Error code: "' . $iErrCode . '"<br />';
                            $this->_sMsg .= 'Error message: "' . $this->_aErrFile[$iErrCode] . '"</p>';
                        }
                    }
                    //else
                    {
                    //    $this->_sMsg .= '<p style="color:red; font-weight:bold; text-align:center">File type incompatible. Please save the image in .jpg, .jpeg, .png or .gif</p>';
                    }
                }
            }
        }
        else
        {
            $this->_sMsg = '<p style="color:red; font-weight:bold; text-align:center">You must select at least one file before submitting the form.</p>';
        }

        return $this;
    }   

    /**
     * 中断文件上传
     *
     * @return object this
     */
    public function cancel()
    {
        if (!empty($_SESSION[$this->_sProgressKey]))
            $_SESSION[$this->_sProgressKey]['cancel_upload'] = true;

        return $this;
    }

     /**
     * @return 上传进度
     */
    public function progress()
    {
        if(!empty($_SESSION[$this->_sProgressKey]))
        {
            $aData = $_SESSION[$this->_sProgressKey];
            $iProcessed = $aData['bytes_processed'];
            $iLength    = $aData['content_length'];
            $iProgress  = ceil(100*$iProcessed / $iLength);
        }
        else
        {
            $iProgress = 100;
        }

        return $iProgress;
    }

     /**
     * @return object this
     */
     public function show()
     {
         ob_start();
         echo '<p><strong>$_FILES Result:</strong></p><pre>';
         var_dump($_FILES);
         echo '</pre>';
         echo '<p><strong>$_SESSION Result:</strong></p><pre>';
         var_dump($_SESSION);
         echo '</pre>';
         $this->_sMsg = ob_get_clean();

         return $this;
     }

    /**
     * Get the JSON informational message.
     *
     * @param integer $iStatus, 1 = success, 0 = error
     * @param string $sTxt
     * @return string JSON Format.
     */
     public static function jsonMsg($iStatus, $sTxt)
     {
         return '{"status":' . $iStatus . ',"txt":"' . $sTxt . '"}';
     }

    /**
     * Get the informational message.
     *
     * @return string
     */
    public function __toString()
    {
        return $this->_sMsg;
    }
}

执行文件上传操作

<?php
require_once ("upload.class.php");
$upload = new CUpload;
$upload->addFile();

echo $upload;
/*$_SESSION["upload_progress_testUpload"] = array(
 "start_time" => 1234567890,   // 请求时间
 "content_length" => 57343257, // 上传文件总大小
 "bytes_processed" => 453489,  // 已经处理的大小
 "done" => false,              // 当所有上传处理完成后为TRUE
 "files" => array(
  0 => array(
   "field_name" => "file1",       // 表单中上传框的名字
   // The following 3 elements equals those in $_FILES
   "name" => "foo.avi",
   "tmp_name" => "/tmp/phpxxxxxx",
   "error" => 0,
   "done" => true,                // 当这个文件处理完成后会变成TRUE
   "start_time" => 1234567890,    // 这个文件开始处理时间
   "bytes_processed" => 57343250, // 这个文件已经处理的大小
  ),
  // An other file, not finished uploading, in the same request
  1 => array(
   "field_name" => "file2",
   "name" => "bar.avi",
   "tmp_name" => NULL,
   "error" => 0,
   "done" => false,
   "start_time" => 1234567899,
   "bytes_processed" => 54554,
  ),
 )
);*/
?>

获取文件上传进度

<?php
session_start();

require_once ("upload.class.php");
echo (new CUpload())->progress();
?>

版权声明:本文为博主郎涯工作室原创文章,未经博主允许不得转载。

时间: 2024-10-05 19:42:06

PHP 获取文件上传进度的相关文章

获取文件上传进度

html代码如下: 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, min

Java使用comms-net jar包完成ftp文件上传进度的检测功能

本文章只讲述大致的思路与本次功能对应的一些开发环境,具体实现请结合自己的开发情况,仅供参考,如果有不对的地方,欢迎大家指出! 准备环境:JDK1.7 OR 1.8.eclipse.ftp服务器(可自行搭建).comms-net jar包3.3版本的.其余的就不详细列举了. 1.在现实开发中ftp服务器和项目的部署服务器基本不会是同一台,所以基础springmvc的文件上传进度获取,只能获取到文件的二进制流到达项目后台的进度.对于真实的ftp文件上传进度,需要使用comms-net提供的监听器来实

ZendFramework2学习笔记 文件上传、文件上传进度

修改php.ini以适应文件的要求: //php.ini file_uploads = On post_max_size = 600M upload_max_filesize = 600M session.upload_progress.enabled = On session.upload_progress.freq = "1%" session.upload_progress.min_freq = "1" 以上我们限制了文件大小限制在不超过600MB. 关于文件

基于HT for Web矢量实现HTML5文件上传进度条

在HTML中,在文件上传的过程中,很多情况都是没有任何的提示,这在体验上很不好,用户都不知道到时有没有在上传.上传成功了没有,所以今天给大家介绍的内容是通过HT for Web矢量来实现HTML5文件上传进度条,矢量在<矢量Chart图表嵌入HTML5网络拓扑图的应用>一文中已经讲述了关于setCompType()方法的应用,今天我们用setImage()方法充分利用系统中定义好的矢量资源来实现文件上传进度条,我们先来看下效果图: 从效果图可以看到,向服务器上传了一个mp4文件,并在最下方显示

Web---文件上传-用apache的工具处理、打散目录、简单文件上传进度

我们需要先准备好2个apache的类: 上一个博客文章只讲了最简单的入门,现在来开始慢慢加深. 先过渡一下:只上传一个file项 index.jsp: <h2>用apache的工具处理文件上传</h2> <!-- 先过渡一下:只上传一个file项 --> <form action="<%= request.getContextPath() %>/upload" method="post" enctype=&quo

struts2实现文件上传进度条(前端JS+Java)(自我整理)

需要做一个文件上传进度的效果,结合网上资料和自己的实践后,这里做一个整理 步骤如下: 1.重写.自定义JakartaMultiPartRequest类 <span style="font-size:12px;">package com.hikvision.fileUploadProcess.interceptor; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import

spring定时任务-文件上传进度条

spring定时任务 导依赖 <!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </depen

Struts2 文件上传 进度条显示

参考成功博客:http://blog.sina.com.cn/s/blog_bca9d7e80101bkko.html 待测试博客:http://blog.csdn.net/z69183787/article/details/52536255 Struts2 文件上传 进度条显示

JQuery和Servlet实现文件上传进度条,能显示上传速度,上传的百分比等

原文:JQuery和Servlet实现文件上传进度条,能显示上传速度,上传的百分比等 源代码下载地址:http://www.zuidaima.com/share/1550463319542784.htm JQuery和Servlet实现文件上传进度条,能显示上传速度,上传的百分比等