简体   繁体   English

使用php将多个文件上传到远程FTP服务器

[英]Upload Multiple files to remote FTP Server using php

I need to use php to upload to an ftp server 4 files. 我需要使用php将4个文件上传到ftp服务器。 I have the following example code, that I am working from. 我有以下示例代码正在使用。 How could this code be changed to upload multiple files that were already on the server (not uploaded at the time of the ftp transfer). 如何更改此代码以上传服务器上已经存在的多个文件(在ftp传输时未上传)。

Lets say I have 4 files in a subfolder relative to the php file that does the upload, lets call the subfolder “/fileshere/” with the following 4 files in it: 假设相对于进行上传的php文件,我在子文件夹中有4个文件,我们将其中包含以下4个文件的子文件夹称为“ / fileshere /”:

file1.zip file2.zip file3.zip file4.zip file1.zip file2.zip file3.zip file4.zip

I need the script to upload each of the files, then give a done message. 我需要脚本来上传每个文件,然后给出完成的消息。

Below is the starting code I am using and trying to adapt. 以下是我正在使用并尝试适应的起始代码。 Any help would be much appreciated: 任何帮助将非常感激:

$ftp_server = "ftp.yourserver.com";
$ftp_user_name = "ftpuser";
$ftp_user_pass = "ftppassword";
$remote_dir = "/target/folder/on/ftp/server";

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = @ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

//default values
$file_url = "";

if($login_result) {
//set passive mode enabled
ftp_pasv($conn_id, true);

//check if directory exists and if not then create it
if(!@ftp_chdir($conn_id, $remote_dir)) {
//create diectory
ftp_mkdir($conn_id, $remote_dir);
//change directory
ftp_chdir($conn_id, $remote_dir);
}

$file = $_FILES["file"]["tmp_name"];
$remote_file = $_FILES["file"]["name"];

$ret = ftp_nb_put($conn_id, $remote_file, $file, FTP_BINARY, FTP_AUTORESUME);
while(FTP_MOREDATA == $ret) {
$ret = ftp_nb_continue($conn_id);
}

if($ret == FTP_FINISHED) {
echo "File '" . $remote_file . "' uploaded successfully.";
} else {
echo "Failed uploading file '" . $remote_file . "'.";
}
} else {
echo "Cannot connect to FTP server at " . $ftp_server;
}

Try this code 试试这个代码

<?php
// connect and login data
$web = 'www.website.com';
$user = 'admin';
$pass = 'password';
// file location
$server_file = '/public_html/example.txt';
$local_file = 'example.txt';
//connect
$conn_id = ftp_connect($web);
$login_result = ftp_login($conn_id,$user,$pass);
//uploading
if (ftp_put($conn_id, $server_file, $local_file, FTP_BINARY))
 {echo "Success";} 
else {echo "Failed";}
?>

if you want to upload multiple files just put your files names into array then put whole code into for loop . 如果要上传多个文件,只需将文件名放入数组,然后将整个代码放入for循环。

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

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