Home  >  Article  >  Backend Development  >  PHP file upload progress bar implementation program_PHP tutorial

PHP file upload progress bar implementation program_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:11:23810browse

There are many ways to implement upload progress bars in PHP. For example, ajax is now the mainstream or using iframe to achieve it. Now we will introduce PHP's apc and uploadprogress to achieve the effect of file upload progress bar.

There are currently two methods I know of. One is to use the APC extension module written by Rasmus Lerdorf, the founder of PHP (http://pecl.php.net/package/apc) , another method is to use the PECL extension module uploadprogress (http://pecl.php.net/package/uploadprogress). I will give two examples of separate implementations for reference. More flexible applications can be modified according to your own needs.


APC implementation method:

Install APC, refer to the official documentation for installation, you can use the PECL module installation method to quickly and easily, not explained here
Configure php.ini, set the parameter apc .rfc1867=1, enables APC to support the upload progress bar function, which is explained in the APC source code documentation
Code example:

The code is as follows Copy code
if ($_SERVER['REQUEST_METHOD'] == 'POST ') { //Upload request
 代码如下 复制代码
if ($_SERVER['REQUEST_METHOD'] == 'POST') {  //上传请求
    $status = apc_fetch('upload_' . $_POST['APC_UPLOAD_PROGRESS']);
    $status['done'] = 1;
    echo json_encode($status);  //输出给用户端页面里的ajax调用,相关文档请自己寻找
    exit;
} elseif (isset($_GET['progress_key'])) {   //读取上传进度
    $status = apc_fetch('upload_'.$_GET['progress_key']);
    echo json_encode($status);
    exit;
} else {
    //其他代码,比如上传表单等
}
$status = apc_fetch('upload_' . $_POST['APC_UPLOAD_PROGRESS']);

$status['done'] = 1;
echo json_encode($status) ; // Output to the ajax call in the client page, please find relevant documents yourself
exit;

} elseif (isset($_GET['progress_key'])) { //Read the upload progress

$status = apc_fetch('upload_'.$_GET['progress_key']);

echo json_encode($status);
 代码如下 复制代码

if($_SERVER['REQUEST_METHOD']=='POST') {
    if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {
        $upload_dir = 'your_path/';
        $ext        = strrchr($_FILES['video']['name'], '.');
        $sessid     = $_POST['UPLOAD_IDENTIFIER'] ;
        $tmpfile    = $upload_dir . $sessid; 
        $sessfile   = $upload_dir . $sessid .$ext;
        if (move_uploaded_file($_FILES['upfile']['tmp_name'],$tmpfile)) {
            //上传成功
        } else {
            //上传失败
    } else {
        //上传错误
       
} elseif (!empty($_GET['sessid'])) {
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    header("Content-Type:text/html;charset=UTF-8");
 
    $unique_id = $_GET['sessid'];
    $uploadvalues = uploadprogress_get_info($unique_id);
 
    if (is_array($uploadvalues)) {
        echo json_encode($uploadvalues);
    } else {
        //读取进度失败,另外处理逻辑
    }
   
} else {
    //显示上传表单
}

exit;} else { //Other code, such as upload form Wait}
Uploadprogress module implementation method: Use the PECL module installation method to install the module Set uploadprogress.file.filename_template = in php.ini "/tmp/upd_%s.txt"Code example:
The code is as follows Copy code
if($_SERVER['REQUEST_METHOD']=='POST') { if ( is_uploaded_file($_FILES['upfile']['tmp_name'])) {           $upload_dir = 'your_path/';             $ext                                                                                .'); $sessid = $_POST['UPLOAD_IDENTIFIER'] ; $tmpfile = $upload_dir . $sessid; $sessfile = $upload_dir . $sessid .$ext; if ( move_uploaded_file($_FILES['upfile']['tmp_name'],$tmpfile)) {                                                                                                                                                                                                  /Upload error } elseif (!empty($_GET['sessid'])) { header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate" ); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); header("Content- Type:text/html;charset=UTF-8"); $unique_id = $_GET['sessid']; $uploadvalues ​​= uploadprogress_get_info($unique_id); if (is_array($uploadvalues)) { echo json_encode($uploadvalues); } else { //Show upload form}


Integration

Now all that’s left is to hook everything together. You can do this through the progress.php page.


Listing 5. Final progress.php page
                                                           " style="background:#FB7">

The code is as follows Copy code 代码如下 复制代码

$id = uniqid("");
?>

Upload Example




"width: 500px; height: 20px; border: 6px solid red; display:none;">
  
"position: relative; height: 20px; background-color: purple; width: 0%; ">
  


$id = uniqid("");

?>

Upload Example

 代码如下 复制代码
type="submit" value="Upload!"/>