加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何在PHP中获取当前上传的字节?

发布时间:2020-12-13 21:45:33 所属栏目:PHP教程 来源:网络整理
导读:如何记录并获取从html表单上传到 PHP脚本的当前字节?我想稍后将当前进度存储在会话或日志中,以便另一个AJAX脚本可以检索它.这是我当前读取当前字节的代码,但它不起作用. $file_type = $_FILES['Filedata']['type'];$file_name = $_FILES['Filedata']['name'
如何记录并获取从html表单上传到 PHP脚本的当前字节?我想稍后将当前进度存储在会话或日志中,以便另一个AJAX脚本可以检索它.这是我当前读取当前字节的代码,但它不起作用.

$file_type = $_FILES['Filedata']['type'];
$file_name = $_FILES['Filedata']['name'];
$file_size = $_FILES['Filedata']['size'];
$file_tmp = $_FILES['Filedata']['tmp_name'];

 $filePointer = fopen($_FILES['Filedata']['tmp_name'],"rb");
 $rr=0;

 if ($filePointer!=false){
 while (!feof($filePointer)){
     $fileData = fread($filePointer,4096);
     $rr =$rr+strlen($fileData);
     // Process the contents of the uploaded file here...
     $_SESSION['fname']=$rr;
 }

fclose($filePointer);
}

move_uploaded_file ($file_tmp,"files/".$file_name);

解决方法

您无法以这种方式访问??上载的字节,因为在文件已上载之前,脚本不会被调用.

您的服务器需要安装APC或类似的pecl包

http://devzone.zend.com/1812/using-apc-with-php/

或者使用HTML5文件api

http://www.matlus.com/html5-file-upload-with-progress/

HTML5更好,因为您不必在php端更改任何内容,只需设置脚本来接收文件,但需要用户的浏览器支持HTML5文件api.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读