繁体   English   中英

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

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

我需要使用php将4个文件上传到ftp服务器。 我有以下示例代码正在使用。 如何更改此代码以上传服务器上已经存在的多个文件(在ftp传输时未上传)。

假设相对于进行上传的php文件,我在子文件夹中有4个文件,我们将其中包含以下4个文件的子文件夹称为“ / fileshere /”:

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

我需要脚本来上传每个文件,然后给出完成的消息。

以下是我正在使用并尝试适应的起始代码。 任何帮助将非常感激:

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

试试这个代码

<?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";}
?>

如果要上传多个文件,只需将文件名放入数组,然后将整个代码放入for循环。

暂无
暂无

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

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