实现篇
一般情况,用php教程实现上传进度条就下面两种方法:
1.apc扩展(作者是php教程的创始人,5.2后php已经加入apc扩展)
2.pecl扩展模块 uploadprogress
不论是apc还是uploadprogress,都需要编译源码教程,因为原有的php函数根本不可能读取到临时文件夹里的东西。下面来看如何使用以及关键的代码:apc实现方法:
1.安装apc
2.配置php.ini,设置参数 apc.rfc1867=1
3.关键代码:
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;
}
uploadprogress实现方法:
1.使用pecl 安装uploadprogress
2.php.ini里面设置 uploadprogress.file.filename_template = “/tmp/upd_%s.txt”
3.关键代码:
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)) {
//上传成功
}
}
1 2 3