简体   繁体   English

将多个.xml文件上传到ftp服务器

[英]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. 我需要从本地文件夹将多个.XML格式文件上传到ftp服务器,同时根据上传的日期和序列号更改其名称。

Example: 例:

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

Tried using glob to select all the .XML file in the local folder. 尝试使用glob选择本地文件夹中的所有.XML文件。 I've tried to loop the local folder with the .XML files in it. 我试图循环本地文件夹中的.XML文件。

Connections and login to ftp server is OK and uploading a single .XML file without changing the name is also successful. 连接并登录到ftp服务器是可以的,并且成功上传单个.XML文件而不更改名称也可以成功。

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. 但是我需要多个.XML文件的帮助,并且不知道在上传到ftp服务器之前如何更改文件名。

// 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. 我希望将本地文件夹的所有文件上传到ftp服务器,其名称会根据使用序列号上传的日期更改。

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++;
} 

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

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