1

i have this code in html

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

here is my php code

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

now i want to store this two files but i cant. i think i have to pass it through array element. if yes then please tell me how is it possible

    please help me to do this
  • possible duplicate of [How to handle multiple file upload using PHP](http://stackoverflow.com/questions/2233816/how-to-handle-multiple-file-upload-using-php) – John Conde Mar 31 '14 at 13:32

3 Answers3

3

try this instead:

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

of course add as many <input> as you want

PHP

your files will be available in the $_FILES array. So you can simply do something like this:

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

hope this helps

ponciste
  • 2,188
  • 11
  • 16
1

Change the name attribute not id and it will work.

Or meaby You could use something like that (HTML5 only): How can I select and upload multiple files with HTML and PHP, using HTTP POST?

Community
  • 1
  • 1
Uriziel
  • 179
  • 1
  • 10
  • but i want to use different input control like in my question like this

    – user3481256 Mar 31 '14 at 13:55
  • again i want to know file extension which gonna be uploaded so how can i find it i had tried this code $ext=explode('.',$_FILES['file']['tmp_name'][$i]); – user3481256 Mar 31 '14 at 16:09
0

User array try name in form file element like this

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

And you will get upload data in $_FILES as array

Girish
  • 11,254
  • 3
  • 32
  • 48
  • but i want to use different input control like in my question like this

    – user3481256 Mar 31 '14 at 13:56
  • Meaby I didnt described it in the right way, sure thing You can edit the `id` of the field but You cant keep them both with name `file` You have to for example change it to `field[]` so they will come to backend as array or just give them different names like `field1` `field2`. I hope that now everything is crystal clear. – Uriziel Mar 31 '14 at 14:01
  • again i want to know file extension which gonna be uploaded so how can i find it i had tried this code $ext=explode('.',$_FILES['file']['tmp_name'][$i]); – user3481256 Mar 31 '14 at 16:09