實現篇
一般情況,用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