简体   繁体   English

我的 php 脚本没有正确上传带有 ftp 的文件

[英]My php script does not upload my file with ftp correctly

In my php code below, i am trying to upload a file.在我下面的 php 代码中,我正在尝试上传文件。 I have adapted this code from one of the posts on the subject.我从有关该主题的其中一篇文章中改编了这段代码。 The file i have to upload has a serial number as part of the name, which is incremented everyday.我必须上传的文件有一个序列号作为名称的一部分,每天递增。 So I use the glob function to put the file name in an array and loop through the array.所以我使用 glob function 将文件名放在一个数组中并循环遍历该数组。 The script works but the file created on the server is empty.该脚本有效,但在服务器上创建的文件为空。 I have seen a response to my question, which is to use我看到了对我的问题的回答,即使用

 ftp_pasv($conn_id, true);

But I do have this in the script, it does not work for me.但是我在脚本中确实有这个,它对我不起作用。 I hope someone can assist me locate what the problem is.我希望有人可以帮助我找到问题所在。 I am working in ubuntu 18.04, also connecting to a unix machine.我在 ubuntu 18.04 工作,还连接到一台 unix 机器。 As an extra question, how would I delete the local file after transfer.作为一个额外的问题,我将如何在传输后删除本地文件。 Thanks for understanding.感谢您的理解。 The code is below.代码如下。

#!/usr/bin/php

<?php

 //define some variables

  $ftp_server='server-address';

 //set up basic connection
  $conn_id = ftp_connect($ftp_server);
  $ftp_user_name="myuser";
  $ftp_user_pass="mypasswd";

  $remote_path = "/";
  $local_path="./";
  chdir($local_path);

   $local_file = glob("$local_path/OBS*");
   $arrlength=count($local_file);

    $cur_dir= getcwd();
    echo $cur_dir . "\n";
   //exit;

 //login with username and password
  $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
  ftp_pasv($conn_id, true);
  print_r(ftp_nlist($conn_id,"."));

 for($x=0;$x<$arrlength;$x++){
  //upload $local_file and save to $remote_path
  if(ftp_put($conn_id, "$local_path/$local_file[$x]", $remote_path, FTP_ASCII)){
    echo "Successfully written to $remote_path)\n";
  }
   else {
    echo "There was a problem\n";
  }
 }
 //close the connection
 ftp_close($conn_id);

?>

try like this像这样尝试

if(ftp_put($conn_id, "$local_path/$local_file[$x]", $remote_path, FTP_BINARY)){
    echo "Successfully written to $remote_path)\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM