简体   繁体   English

PHP将多个文件上传到FTP

[英]PHP upload multiple files to FTP

I'm trying to upload files to FTP through PHP, and it works... sort of. 我正在尝试通过PHP将文件上传到FTP,它可以工作......有点儿。 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循环遍历从表单中获取的所有文件,例如:

foreach($_FILES['files'] as $file){
    // your upload logic here
}

You will also have to adjust the logic in your html upload form. 您还必须调整html上传表单中的逻辑。 You have to account for multiple $_FILES['files'] , eg in the format of $_FILES['files'][0] , $_FILES['files'][1] ,..., $_FILES['files'][n] 您必须考虑多个$_FILES['files'] ,例如格式为$_FILES['files'][0]$_FILES['files'][1] ,..., $_FILES['files'][n]

I hope this will give you some direction :-) 我希望这会给你一些方向:-)

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

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