PHP上传脚本损坏文件,文件大小与源文件不同


PHP Upload Script corrupting file, Size of file differs from source file

我正在运行一个php脚本,当有人单击按钮时,该脚本会在服务器上执行。脚本从一台服务器下载文件,将文件存储在本地,然后上传到另一台服务器。

但是文件在中间的某个地方不断损坏——上传的最终文件总是不同的大小,并被损坏。

以下代码:

<?php
//Download CSV Script
// connect and login to FTP and download zip
$ftp_server = "URL REDACTED";
$ftp_conn = ftp_connect($ftp_server, 21) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, "USER", "PASS");
// turn passive mode on
ftp_pasv($ftp_conn, true);
$local_file = "feed/establishmentexport.zip";
$server_file = "establishmentexport.zip";
// download server file
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII))
  {
  echo "Successfully downloaded from X.<br />";
  }
else
  {
  echo "Error downloading feed. Please reload and retry.<br />";
  }
// close connection
ftp_close($ftp_conn);
//End of download.
//FTP UPLOAD SCRIPTS
//Uploading the FTP file EMEA
$ftp_server = "REDACTED";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, "USER", "PASS");
// turn passive mode on
ftp_pasv($ftp_conn, true);
$local_file_u = "feed/establishmentexport.zip";
$server_file_u = "custom/establishmentexport.zip";
// upload file
if (ftp_put($ftp_conn, $server_file_u, $local_file_u, FTP_ASCII))
  {
  echo "Successfully uploaded to EMEA custom folder.<br />";
  }
else
  {
  echo "Error uploading to EMEA custom folder.<br />";
  }
// close connection
ftp_close($ftp_conn);
//End of Uploading the FTP file
?>

FTP_ASCII用于处理文本文件,我认为您应该使用FTP_BINARY来正确处理.zip文件。

替换:

ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII)

带有:

ftp_get($ftp_conn, $local_file, $server_file, FTP_BINARY)

和:

ftp_put($ftp_conn, $server_file_u, $local_file_u, FTP_ASCII)

带有:

ftp_put($ftp_conn, $server_file_u, $local_file_u, FTP_BINARY)