dongwang3066 2013-11-30 15:16
浏览 140
已采纳

使用curl恢复ftp上传

i'm buidling a php script to upload larges files from a local php server to a distant ftp server with log of progress in a mysql base. Evrything work fine, but i get problem with the resume function and i can't find any clear information of the process to follow to resume an ftp upload with curl. my code bellow :

$fp = fopen($localfile, 'r');

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_NOPROGRESS, false);
curl_setopt($curl, CURLOPT_PROGRESSFUNCTION, 'curlProgressCallback'); // lof progress to base
curl_setopt($curl, CURLOPT_READFUNCTION, 'curlAbortCallback'); // check if user request abort

curl_setopt($curl, CURLOPT_URL, $ftp);
curl_setopt($curl, CURLOPT_PORT, FTPPort);
curl_setopt($curl, CURLOPT_LOW_SPEED_LIMIT, 1000);
curl_setopt($curl, CURLOPT_LOW_SPEED_TIME, 20); 
curl_setopt($curl, CURLOPT_USERPWD, FTPLog . ":" . FTPPass);
curl_setopt($curl, CURLOPT_FTP_CREATE_MISSING_DIRS, true); 
curl_setopt($curl, CURLOPT_UPLOAD, 1);

if($resume){
    $startFrom=ftpFileSize($dest); // return the actual file size on the ftp server
}else{         
    $startFrom=false;        
}

if($startFrom){ 
    curl_setopt ($curl, CURLOPT_FTPAPPEND, 1);
    curl_setopt($curl, CURLOPT_RESUME_FROM, $startFrom);       
    fseek($fp, $startFrom, SEEK_SET);
    $sizeToUp=filesize($localfile);//-$startFrom;        
}else{               
    $sizeToUp=filesize($localfile);
}    
curl_setopt($curl, CURLOPT_INFILE, $fp);
curl_setopt($curl, CURLOPT_INFILESIZE, $sizeToUp);

curl_exec($curl); 

If someone call help me on this or redirect me on a valid example it will be very helfull and appreciate.

Tks for your feedback

Mr

  • 写回答

3条回答 默认 最新

  • douqin7086 2013-12-31 12:22
    关注

    So i've abort my research to make resume with curl, not enought help or documentation on it, And it's really more easy to do it with ftp_nb_put. So i f its can help someone, you can find a exemple of my final code bellow :

        define("FTPAdd","your serveur ftp address");                                    
        define("FTPPort",21);                                                           
        define("FTPTimeout",120);
        define("FTPLog","your login");                                                 
        define("FTPPass","your password"); 
    
        function ftpUp_checkForAndMakeDirs($ftpThread, $file) {
                $parts = explode("/", dirname($file));
                foreach ($parts as $curDir) {            
                    if (@ftp_chdir($ftpThread, $curDir) === false) { // Attempt to change directory, suppress errors
                        ftp_mkdir($ftpThread, $curDir); //directory doesn't exist - so make it
                        ftp_chdir($ftpThread, $curDir); //go into the new directory
                    }
                }
        }   
        function ftpUp_progressCallBack($uploadedData){
             global $abortRequested;
             //you can do any action you want while file upload progress, personaly, il log progress in to data base
             //and i request DB to know if user request a file transfert abort and set the  var $abortRequested to true or false
        }
        function ftpUp($src,$file,$dest,$resume){
                global $abortRequested; 
    
                $conn_id = ftp_connect(FTPAdd,FTPPort,FTPTimeout);
                    if ($conn_id==false){
                            echo "FTP Connection problem";return false;                
                    }else{
                            $login_res = ftp_login($conn_id, FTPLog, FTPPass);
                            if ($login_res){
                                    $ftpThread=$conn_id;            
                            }else{
                                    echo "FTP Authentification error";return false;
                            }
                    }
                $fp = fopen($src, 'r');     
    
                ftpUp_checkForAndMakeDirs($ftpThread, $dest); //verif et creation de l'arborescence sur le serveur ftp
                ftp_set_option ($ftpThread, FTP_AUTOSEEK, TRUE); // indispensable pour pouvoir faire du resume
    
                if($resume){            
                    $upload = ftp_nb_fput ($ftpThread, $file, $fp ,FTPUpMode, FTP_AUTORESUME);
                }else{
                    $upload = ftp_nb_fput ($ftpThread, $file, $fp ,FTPUpMode);    
                }       
                ///////////////////////////////////////////////////////////////////////////////////
                //uploading process
                while ($upload == FTP_MOREDATA) {               
                    //progress of upload                
                        ftpUp_progressCallBack(ftell ($fp));                
                    //continue or abort 
                    if(!$abortRequested){
                        $upload = ftp_nb_continue($ftpThread);
                    }else{              
                        $upload = "userAbort";
                    }            
                 }
                 ///////////////////////////////////////////////////////////////////////////////////
                 //end and result                   
                 ftpUp_progressCallBack(ftell ($fp));       
    
                 if ($upload != FTP_FINISHED) {
                     @fclose($fp);
                     @ftp_close ($ftpThread);
                     if ($abortRequested){                              
                        echo "FTP Abort by user : resume needed";
                     }else{
                        echo "FTP upload error : ".$upload." (try resume)";
                     }
                 }else{
                    @fclose($fp);
                    @ftp_close ($ftpThread);
                    echo "upload sucess";
                 }
    
        } 
    
    
        $file="test.zip";
        $src = "FilesToUp/" . $file; 
        $destDir = "www/data/upFiles/";
        $dest = $destDir . $file;
        $abortRequested=false;
    
        ftpUp($src,$file,$dest,true);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'