简体   繁体   中英

My php script does not upload my file with ftp correctly

In my php code below, i am trying to upload a file. 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. 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. 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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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