dongpan1416 2015-06-25 15:56
浏览 43
已采纳

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 2015-06-25 16:11
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据