簡體   English   中英

如何在php中上傳多個文件?

[英]how to upload multiple files in php?

我在html中有此代碼

<input type="file" name="file" id="file1"/><br />
<input type="file" name="file" id="file2"/><br />

這是我的PHP代碼

if(!empty($_FILES['image']['name'])) {
            $ext=explode('.',$_FILES['image']['name']);
            $new_name=$property_id."_".rand(10,10000).".".end($ext);
            $name=SITEPATH.S_LOCATIONS_LARGE.$new_name;
            $small=SITEPATH.S_LOCATIONS.$new_name;
            move_uploaded_file($_FILES['image']['tmp_name'],$name);
            if(is_file($name)) {
                $this->db->update("insert into bloom_location_images (image,property_id) values ('$new_name',$property_id)");
                list($width, $height, $type, $attr) = getimagesize($name);
                if($width > 600) {
                    $this->magic->constrain_by_scale($name,500,0);
                }
                copy($name,$small);
                if($width > 301) {
                    $this->magic->constrain_by_scale($small,301,0);
                }
            }
        }

        return $property_id

現在我想存儲這兩個文件,但我不能。 我想我必須通過數組元素。 如果是的話,請告訴我怎么可能

    please help me to do this

試試這個代替:

<input name="upload[]" type="file" multiple="multiple" />

當然,您可以根據需要添加任意數量的<input>

的PHP

您的文件將在$_FILES數組中可用。 因此,您可以簡單地執行以下操作:

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

    $path = "yourfolder/" . $_FILES['upload']['name'][$i];

    if(move_uploaded_file($tmp, $path)) {
         // your code here
    }
}

希望這可以幫助

更改name屬性而不是id ,它將起作用。

或meaby您可以使用類似的方法(僅HTML5): 如何使用HTTP POST選擇和上傳HTML和PHP多個文件?

像這樣的表單文件元素中的用戶數組嘗試名稱

<input name="image[]" type="file" multiple="multiple" />

並且您將在$_FILES獲得上傳數據作為數組

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM