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 Acrn IVSHMEM doorbell问题
  • ¥15 yolov5中的val测试集训练时数量变小问题
  • ¥15 MPLS/VPN实验中MPLS的配置问题
  • ¥15 materialstudio氢键计算问题
  • ¥15 已知隐函数其中一个变量的,求另外一个变量
  • ¥15 echarts图表制作
  • ¥15 halcon根据玻璃面板纹路取区域
  • ¥15 HFSS设计小型化180度耦合器
  • ¥15 使用CInternetSession,CHttpFile读取网页文件时有些电脑上会卡住怎么办?
  • ¥15 水下机器人的半物理仿真研究