Reputation: 334
I'm trying to upload files to FTP through PHP, and it works... sort of. Please have a look at my code;
$filename = $_FILES['files']['name'];
$host = "ftp.mydomain.com";
$username = "myusername";
$password = "mypassword";
$local_file = 'upload/'.$filename;
$remote_file = $filename;
$con = ftp_connect($host, 21) or die("Couldnt connect");
$log = ftp_login($con, $username, $password) or die("Wrong username or password.");
ftp_pasv($con, true);
$upload = ftp_put($con, $remote_file, $local_file, FTP_BINARY);
if($upload) echo 'Error.';
ftp_close($con);
echo 'Success';
exit;
This script actually work, but just with ONE file. If I'm uploading multiple files through my form, it will just upload one file. I want all of the files from my form to be uploaded. How can I do that?
Upvotes: 1
Views: 1634
Reputation: 2800
You might want to look into loops. One solution could be to loop through all files you get from your form with a foreach loop, e.g.:
foreach($_FILES['files'] as $file){
// your upload logic here
}
You will also have to adjust the logic in your html upload form. You have to account for multiple $_FILES['files']
, e.g. in the format of $_FILES['files'][0]
, $_FILES['files'][1]
,...,$_FILES['files'][n]
I hope this will give you some direction :-)
Upvotes: 3