Simple implementation method of PHP file upload progress bar

WBOY
Release: 2016-07-25 09:00:52
Original
1142 people have browsed it
We provide you with two codes for PHP file upload progress bar for your study and reference.

One is to use the APC extension module: http://pecl.php.net/package/apc. The other is to use the PECL extension module uploadprogress to implement: http://pecl.php.net/package/uploadprogress.

1. APC implementation method: To install APC, refer to the official documentation for installation. You can use the PECL module to install it. Configure php.ini and set the parameter apc.rfc1867=1 to enable APC to support the upload progress bar function. Example:

<?php
/**
APC方法 上传进度条
*/
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 {
//其他代码,比如上传表单等
}
Copy after login

2. Implementation method of uploadprogress module: Install the module using the PECL module installation method Set uploadprogress.file.filename_template = “/tmp/upd_%s.txt” in php.ini Example:

<?php
/**
 上传进度条
 bbs.it-home.org
*/

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 {
//显示上传表单
}
?>
Copy after login

There is code for the upload progress bar, which looks more intuitive and provides a good user experience.



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template