dongpan1416
dongpan1416
2015-06-25 15:56

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

已采纳

I am running a php script that is executed on a server when someone clicks a button. The script downloads the file from one server stores the file locally then uploads it to another server.

But the file keeps corrupting somewhere in the middle the end file that is uploaded is always a different size and gets corrupted.

Code Below:

<?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条回答

  • dongmi19720408 dongmi19720408 6年前

    FTP_ASCII is intended to be used when handling text files, I believe you should use FTP_BINARY in order to properly handle your .zip file.

    Replace:

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

    with:

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

    And:

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

    With:

    ftp_put($ftp_conn, $server_file_u, $local_file_u, FTP_BINARY)
    
    点赞 评论 复制链接分享