PHP通过FTP上传多个文件

问题描述 投票:0回答:3

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

提前致谢

php file-upload ftp
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);
}

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 );
}

1
投票

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

<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>";
                }
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.