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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看