php通过ftp上传多个文件

时间:2012-10-31 12:56:47

标签: php file-upload ftp

我目前正在使用以下代码将图像上传到ftp服务器:

<input name="userfile" type="file" size="50">

...

$filep=$_FILES['userfile']['tmp_name'];
$name=$_FILES['userfile']['name']; 

$upload = ftp_put($conn_id, $paths.'/'.$name, $filep, FTP_BINARY);

这样可以正常工作,只是它只上传一张图片。

任何有关多张(3-4张)图片上传的帮助。

提前致谢

3 个答案:

答案 0 :(得分:3)

<input name="userfile[]" type="file" size="50" />
<input name="userfile[]" type="file" size="50" />
<input name="userfile[]" type="file" size="50" />

for($i = 0; $i < count($_FILES['userfile]); $i++) {
   $filep=$_FILES['userfile']['tmp_name'][$i];
   $name=$_FILES['userfile']['name'][$i]; 

   $upload = ftp_put($conn_id, $paths.'/'.$name, $filep, FTP_BINARY);
}

答案 1 :(得分:1)

你应该逐个上传文件异步:)

HTML:

<input name="userfile[]" type="file" size="50" />
<input name="userfile[]" type="file" size="50" />
<input name="userfile[]" type="file" size="50" />

PHP:

for( $i = 0 ; $i < count( $_FILES['userfile'] ) ; $i++ )
{
   $filep = $_FILES['userfile']['tmp_name'][$i];
   $name = $_FILES['userfile']['name'][$i];

   $upload = ftp_put( $conn_id , $paths . '/' . $name , $filep , FTP_BINARY );
}

答案 2 :(得分:1)

这是我用过的。所有图片字段都具有相同的name属性值files

<input type="file" name="files[]" class="files" multiple />可用于html5,或为非html5添加此行的多个。我使用jquery作为我的上传器,但你可以只看到多个上传字段。

显然你可以改变我的代码来使用你的ftp代码,但这也是另一种方式。

        $files = array();

        $allowed_filetypes = array('.jpg','.jpeg','.gif','.bmp','.png','.tif');
        $max_filesize = 1524288;
        $upload_path = 'images/image_uploads/';

        for ($i = 0; $i < count($_FILES['files']['name']); $i++){
            if($_FILES['files']['name'][$i] != "") {
                $filename = $_FILES['files']['name'][$i];
                $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); 
                $ext = strtolower($ext);

                if(!in_array($ext,$allowed_filetypes))
                    die("The file you attempted to upload ($filename) is not allowed.");

                if(filesize($_FILES['files']['tmp_name'][$i]) > $max_filesize)
                    die("The file you attempted to upload ($filename) is too large.");

                if(!is_writable($upload_path))
                    die("You cannot upload to the specified directory, please CHMOD it to 777.");

                $ran = rand();
                $filename = $ran.$ext;
                if(move_uploaded_file($_FILES['files']['tmp_name'][$i],$upload_path.$filename)) {
                    $result = mysql_query("Insert Into image_uploads_images (upload_id, image, original_name) Values ('$id', '$filename', '".$_FILES['files']['name'][$i]."');");

                    if($result){
                        array_push($files, "http://www.site.com/images/image_uploads/$filename => ".$_FILES['files']['name'][$i]);
                    }else{
                        echo "<p style=\"color:#cc3333;\">Unable to upload ".$_FILES['files']['name'][$i]."</p>";
                    }
                }else{
                    echo "<p style=\"color:#cc3333;\">Unable to upload ".$_FILES['files']['name'][$i]."</p>";
                }
            }
        }