user3233664
user3233664

Reputation:

php images upload does not work

I am trying to figure out why i am getting a bunch of errors when trying to upload multiple images

the input is <input type='file' name='file[]' multiple>

the check is:

if (!empty($_FILES['file'])){
    if ($_FILES['file']['name'] != "") {
        $count = 0;       
        foreach ($_FILES['file']['name'] as $filename) {
            $allowed = array('jpg', 'jpeg', 'png', 'bmp', 'tiff', 'gif');
            $img_name = $filename;
            $img_extn = strtolower(end(explode('.', $img_name)));
            $img_temp = $_FILES['file']['tmp_name'][$count];
            $count = $count + 1;
            if(in_array($img_extn, $allowed) === true){         
                $img_path = 'images/images/' . substr(md5(time()), 0, 10) . '.' . $img_extn;
                move_uploaded_file($img_temp, $img_path);       

                $images = array(
                'images'    => $img_path,
                'post_id'       => $_POST['id']
                ); 
                add_images($images);    

            }else{
                $errors[] = ''.$filename.' - file type is not allowed. Only: ' . implode(', ', $allowed) . '';                                      
            }
        }
    }
}

only one image is being uploaded to the temp folder and how can i connect the post_id to the database?

Upvotes: 0

Views: 81

Answers (3)

ponciste
ponciste

Reputation: 2229

Why don't you use $filename inside the foreach instead of $_FILES['file']['name']?

Upvotes: 2

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

$img_name = $_FILES['file']['name'];

should be

$img_name = $filename; 

since you are using for-each

foreach ($_FILES['file']['name'] as $filename) {
}

Upvotes: 1

Anoop Santhanam
Anoop Santhanam

Reputation: 85

Multiple images. it has to be $_FILES['file']['name'][0]

Use indexes to access it. Since I see that you haven't used indexes. explode() throws a warning because it's trying to break an array.

Upvotes: 0

Related Questions