文件上传在上传过程中中止

时间:2014-05-12 03:35:22

标签: php ajax curl

关于如何在我的团队项目中上传流程的简要说明。

  

在客户端,我们通过AJAX将上传服务调用到应用服务器。然后,应用程序服务器通过Curl将该文件转发到文件服务器(文件服务器是私有的,只能由应用程序服务器访问)

情况是这样的。

  

当文件上传时,它已经通过应用程序服务器并已到达Fileserver。但在数据传回客户端之前,用户单击客户端的取消按钮。

如何从应用程序服务器检查,如果用户中止请求,如果已经上传,请将删除调用到文件服务器?

我的解决方案

  1. 如果php设置ignore_user_abort=false,我无法检查上传是否已取消。所以我在curl之前将它设置为true

    ini_set('ignore_user_abort', TRUE);
    

    **顺便说一下,即使在脚本调用中止后,ignore_user_abort=false仍然不会立即终止卷曲执行。

  2. 如果呼叫中止,请设置CURLOPT_NOPROGRESS以跟踪进度。

    curl_setopt($ch, CURLOPT_NOPROGRESS, false);
    curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'callback_progress');
    
  3. 处理进度

    $cancelled = false; //cancel flag
    
    function callback_progress($download_size, $downloaded, $upload_size, $uploaded){
    
        //Really need this, if not connection_aborted() never be true and curl still running even if script aborted
        print " "; ob_flush (); flush ();
    
        if(connection_aborted()!= 0){
            if(!$cancelled) $cancelled = true;
            return 0; //to continue script, and handle later
        }
    }
    
  4. 继续

    curl_close($ch); //close curl
    ini_set('ignore_user_abort', FALSE); //set back to false
    
    //If $cancelled if true, make delete call to file server using the file id 
    if($cancelled && isset($response['id'])) return $this->removeFile($response['id']);
    
  5. 但是,它不起作用。 $cancelled仍然是假的,虽然它在callback_progress函数中已经存在。

    有更好的方法吗?在这种情况下,我无法在网络的任何地方找到合适的解决方案。

1 个答案:

答案 0 :(得分:0)

我认为这是$cancelled变量的一个案例,并且对removeFile方法的调用不可用,因为在您的进度函数调用时,您输入的脚本部分将在第一次调用callback_progress之前已经运行了

尝试检查callback_progress中是否$cancelledrequest['id']。这样removeFile函数应该在$cancelled确实为真时运行

相关问题