PHP 大文件上传进度条实现

  目前我知道的方法有两种,一种是使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc),另外一种方法是使用PECL扩展模块uploadprogress实现(http://pecl.php.net/package/uploadprogress) 我这里举两个分别实现的例子供参考,更灵活的应用根据自己需要来修改。

  APC实现方法:

  • 安装APC,参照官方文档安装,可以使用PECL模块安装方法快速简捷,这里不说明
  • 配置php.ini,设置参数 apc.rfc1867=1 ,使APC支持上传进度条功能,在APC源码说明文档里面有说明
  • 代码范例:
    [coolcode lang=”php”]
    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 {
    //其他代码,比如上传表单等
    }
    [/coolcode]

  uploadprogress 模块实现方法:

  • 使用PECL模块安装方法安装该模块
  • php.ini里面设置 uploadprogress.file.filename_template = “/tmp/upd_%s.txt”
  • 代码范例:
    [coolcode lang=”php”]
    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 {
    //显示上传表单
    }
    [/coolcode]

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
19 Responses
  1. jason says:

    有支持断点的模块的吗?

  2. Michael says:

    [Comment ID #30165 Will Be Quoted Here]

    断点续传这事儿,需要客户端来支持,仅靠服务器端,服务做到。

  3. Fisher says:

    在JSP中,文件上传进度条如何实现?

  4. Michael says:

    [Comment ID #30207 Will Be Quoted Here]

    还真不知道,原来折腾tomcat和resin的时候,没有研究过这个,我想应该有这方面的开源组件的,baidu或者google试试吧。

  5. Fisher says:

    谢谢你的答复。

    在网上有一些用AJAX或AS实现的,用了一下没到达到想要的结果。

  6. kakashilw says:

    hi…请问你的blog的代码高亮用的是哪个插件啊???

  7. Michael says:

    [Comment ID #30954 Will Be Quoted Here]

    coolcode 好像作者网站是 http://www.coolcode.cn ,是个牛人。

  8. kakashilw says:

    很好用。。。3Q

  9. Time is the great author, she can write the end of the future. -Chaplin

  10. Love is patient; love is kind; love is not envious pr boastful or arroga nt or rude. It does not insist on its own way; it is not irritable or re sentful; it does not rejoice in wrongdoing, but rejoices in the truth. I t bears all things, hopes all things, endures all things. Love never ends.

  11. Hallelujah! For the Lord our God the Almighty reigns. The kingdom of the world has become the kingdom of our Lord and of his Messiah, and he wil l reign forever and ever.

  12. A man can fail many times, but he isn’t a failure until he begins to blame somebody else.

    J. Burroughs. Averican naturalist

  13. ing forward to read other articles.

  14. To improve their ability, to benefit oneself and him. Have the ability to do practical things won’t talk any benefits after all. The end of the story always return fully loaded, is a nurse nomination.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image