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

时间:2015-06-25 15:56:00

标签: php

当我有人点击按钮时,我正在运行一个在服务器上执行的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

?>

1 个答案:

答案 0 :(得分:1)

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)