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


Why would uploading a zip file via ftp_fput return true but not upload the file?

以下命令返回true并将文本XML文件上传到FTP服务器:

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

然而,当我尝试上传一个包含文本XML文件的.zip文件时,它仍然返回true,但没有上传文件:

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

我发现,如果我简单地将zip文件重命名为".xml",它会上传文件,但.zip文件已损坏。

但是,如果我将zip文件重命名为".zip.xml",它会再次返回true,但不会上传文件。

这种奇怪行为的原因是什么

其他信息:

zip文件可以通过FileZilla上传,使用相同的帐户没有问题。

我还指定:

ftp_pasv($this->ftpConnectionId, true);
压缩文件是一个二进制文件。这可能就是将其上传为.xml会损坏文件的原因。请尝试指定FTP_BINARY而不是FTP_ASCII。FTP_BINARY也适用于ascii文件,但反之亦然,因此您可以始终使用FTP_BINARY而不是始终使用FTP-ascii。

ftp服务器可能会因为多种原因拒绝该文件,因此它可能首先允许上传,但随后不保存该文件。ascii/二进制可能是一个问题,但一些文件扩展名可能被列入黑名单,或者文件可能太大。不过,后者不太可能,因为上传带有不同扩展名的zip文件对您很有用。

我认为ftp服务器会主动忽略zip文件。

这是因为zip文件包含文件,并且可能是。如果大小大于XMl

我们使用此代码通过ftp 上传整个目录

试试这个代码。这将适用于您的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;
            }