0

I am trying to make a multiple file upload page using a single input. But when I upload it show this message

Undefined index: files in C:\xampp\htdocs\tapromtour.com\admin_panel\view\gallery_script.php on line 13

This is my upload form code:

<form action="gallery_script" method="POST" enctype="multipart/form-data" style="width:950px;text-align:center;">
 <br/><br/>
<input name="title" type="text" placeholder="Give the album name" style="padding:5px; width:500px;" required ="required"/><br/><br/>
 <input type="file" name="attachPhoto1[]" multiple="multiple" required="required"/>
 <br/><br/>
 <input type="submit" name="submit" value="Submit" id="submit" />
</form>

And this is my upload php page:

<?php
if (isset($_POST['submit'])){
    //file uplaod
   $title = $_POST['title'];
   if(!empty($_FILES["attachPhoto1"]['name'])) {
        $allowedExts = array("gif", "jpeg", "jpg", "png");
        $error_uploads = 0;
        $total_uploads = array();
        $upload_path = '../album/';
        foreach($_FILES["attachPhoto1"]['name'] as $key => $value) {
            $temp = explode(".", $_FILES["attachPhoto1"]['name'][$key]);
            $extension = end($temp);
            if ($_FILES["files"]["type"][$key] != "image/gif"
                && $_FILES["files"]["type"][$key] != "image/jpeg"
                && $_FILES["files"]["type"][$key] != "image/jpg"
                && $_FILES["files"]["type"][$key] != "image/pjpeg"
                && $_FILES["files"]["type"][$key] != "image/x-png"
                && $_FILES["files"]["type"][$key] != "image/png"
                && !in_array($extension, $allowedExts)) {
                $error_uploads++;
                continue;
            }
            $file_name = time().rand(1,5).rand(6,10).'_'.str_replace(' ', '_', $_FILES["attachPhoto1"]['name'][$key]);
            if(move_uploaded_file($_FILES["attachPhoto1"]['tmp_name'][$key], $upload_path.$file_name)) {
                $total_uploads[] = $file_name;
                mysql_query ("INSERT INTO gallery (id, image, title, date) VALUE ('','$file_name','$title',now())");
                header("Location: gallery");
            } else {
                $error_uploads++;
            }
        }
        if(sizeof($total_uploads)) {


        }
        }
    }    

//file uplaod
?>

I don't know what is wrong in the above code. It looks fine but it shows the message when I upload the images. Can you tell me what wrong with the above code? Thanks for helping.

  • `if ($_FILES["files"]["type"][$key] != "image/gif"` .. should just be `if ($extension != "image/gif"` ... –  Jan 10 '17 at 00:50
  • Thanks nogad, but I did it just now and it did not help. – Sreng Pagna Jan 10 '17 at 00:54
  • You are using `foreach( ... as $key => $value)`, then go and use `$key` and `$value`. Use print_r($_FILES) to get an idea of what the structure of that array is. – RST Jan 10 '17 at 00:57
  • Sorry RST, but I don't understand what you want me to do. Can you explain more clearly? – Sreng Pagna Jan 10 '17 at 01:05
  • $_FILES["files"] does not exit your index is 'attachPhoto1' if you copy code from random sites you have to change the variables appropriately. –  Jan 10 '17 at 01:06
  • Thanks nogad. That works for me. I change from files to attachPhoto1 – Sreng Pagna Jan 10 '17 at 01:10

0 Answers0