简体   繁体   中英

upload multiple .xml files to a ftp server

I need to upload multiple .XML format file to a ftp server from a local folder with changing their names according to the date uploaded and serial number simultaneously.

Example:

  • 20190814-00000001-1.xml
  • 20190814-00000002-2.xml
  • 20190814-00000003-3.xml

Tried using glob to select all the .XML file in the local folder. I've tried to loop the local folder with the .XML files in it.

Connections and login to ftp server is OK and uploading a single .XML file without changing the name is also successful.

But i need help with multiple .XML files and have no idea how i am going to change the name of the files before uploading to the ftp server.

// connection info
$usr = 'ftp_usrname';
$pwd = 'ftp_password';
$ftp_server = "ftp_server";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $usr, $pwd);  
ftp_pasv($ftp_conn, true) or die("Cannot switch to passive mode");  

foreach (glob("xml/*.xml") as $filename) {      // local files folder
    $ftp_path = 'test1/';                       // ftp folder to save the files
    ftp_put($ftp_conn,$ftp_path , $filename, FTP_BINARY);
}     

// close connection
ftp_close($ftp_conn);

I want all the files of the local folder, uploaded to the ftp server with the names changes according to the date uploaded with a serial number.

Presuming your code to upload a single file works, to change file name you can do:

$counter = 1;
foreach (glob("xml/*.xml") as $filename) {      // local files folder
    $ftp_path = 'test1/';                       // ftp folder to save the files

    $serial = str_pad($counter, 8, "0", STR_PAD_LEFT);
    $new_file_name = date("Ymd") . "-{$serial}-{$counter}.xml";
    ftp_put($ftp_conn, $ftp_path . $new_file_name, $filename, FTP_BINARY);

    $counter++;
} 

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