0

我目前正在使用以下代码将图像上传到 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)图像上传的帮助。

提前致谢

4

3 回答 3

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);
}
于 2012-10-31T13:03:02.833 回答
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 );
}
于 2012-10-31T13:02:10.160 回答
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>";
                }
            }
        }
于 2012-10-31T13:04:41.587 回答