doumisha5081
doumisha5081
2012-09-07 09:01

为什么通过ftp_fput上传zip文件会返回true但不上传文件?

已采纳

The following command returns true and uploads the text XML file to the FTP server:

if (ftp_put($this->ftpConnectionId, $this->remoteXmlFileName, $this->localXmlFileName, FTP_ASCII)) {

However, when I try to upload a .zip file intead of a text XML file, it still returns true but does not upload the file:

if (ftp_put($this->ftpConnectionId, $this->remoteXmlFileName, $this->localXmlFileName, FTP_BINARY)) {

I found that if I simply rename the zip file to ".xml", it WILL upload the file but the .zip file is corrupted.

But if I rename the zip file to ".zip.xml" it again returns true but does not upload the file.

What could be the reasons for this odd behavior?

Additional Info:

A zip file can be uploaded via FileZilla no problem with the same account.

I also am specifing:

ftp_pasv($this->ftpConnectionId, true);
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dt3358 dt3358 9年前

    A zipfile is a binary file. That's probably why uploading it as .xml corrupts the file. Try specifying FTP_BINARY instead of FTP_ASCII. FTP_BINARY will work for ascii files too, but not vice versa, so you can better always use FTP_BINARY than always FTP_ASCII.

    The ftp server may reject the file for many reasons, so it may allow the upload at first, but then not save the file. The ascii/binary problem may be one, but also some file extensions may be blacklisted, or the file could be too big. The latter is unlikely, though, since uploading the zipfile with a different extensions worked for you.

    I think the ftp server actively ignores zip files.

    点赞 评论 复制链接分享
  • doutangliang7769 doutangliang7769 9年前

    This is because zip file contains Files and may be is size is greater than your XMl

    We have used this code to upload entire directory over ftp

    Try this code. This will work for your ftp

    //Start ftp upload code
        $ftp_user_name =$_SESSION['upload']['username'];
        $ftp_user_pass = $_SESSION['upload']['password'];
        $ftp_server = $_SESSION['upload']['host'];
        $sourcepath = $_SESSION['upload']['source'];
        $dest_folder = $_SESSION['upload']['dest_folder'];          
        $conn_id = @ftp_connect($ftp_server,21) or die("Couldn't connect to $ftp_server");
        if (@ftp_login($conn_id, $ftp_user_name, $ftp_user_pass)) {
            ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 70000000000000000); // Set the network timeout to 10 seconds
            ftp_copyAll($conn_id, $sourcepath, $dest_folder);
    
           }
    
    
          function ftp_copyAll($conn_id, $src_dir, $dst_dir) {
                    if(is_dir($dst_dir)){
                        return "Dir $dst_dir Already exists";
                    } else {
                        $d = dir($src_dir);
                        ftp_mkdir($conn_id, $dst_dir); //echo "creat dir $dst_dir";
                        while($file = $d->read()) { // do this for each file in the directory
                            if ($file != "." && $file != "..") { // to prevent an infinite loop
                                if (is_dir($src_dir."/".$file)) { // do the following if it is a directory
                                $src_dir_path=$src_dir."/".$file;
                                $dst_dir_path=$dst_dir."/".$file;
    
                                ftp_copyAll($conn_id, $src_dir_path, $dst_dir_path); // recursive part
                                } else {                        
                                    $upload = ftp_put($conn_id, $dst_dir."/".$file, $src_dir."/".$file, FTP_BINARY); // put the files
                                    //echo "creat files::: ".$dst_dir."/".$file ."";
                                    echo " ";
                                }
                            }
                            ob_flush() ;
                            flush(); 
                            usleep(90000);
                            //sleep(1);
                        }
                        $d->close();
                    }
                     return true;
                }
    
    点赞 评论 复制链接分享

相关推荐