简体   繁体   中英

PHP upload multiple files to FTP

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?

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 , eg:

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'] , eg in the format of $_FILES['files'][0] , $_FILES['files'][1] ,..., $_FILES['files'][n]

I hope this will give you some direction :-)

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