0

I'm a beginner in php and now doing a project in php. I want to upload images(maximum four image files only) .I used the following code to upload images.

<?php   if(isset($_POST['submit'])) {
    $count=count($_FILES["images"]["name"]);


for($i=0;$i<$count;$i++) {      if ((($_FILES["images"]["type"][$i] == "image/gif")  || ($_FILES["images"]["type"][$i]  == "image/jpeg") || ($_FILES["images"]["type"][$i]  == "image/pjpeg")) && ($_FILES["images"]["size"][$i] < 100000)) {

 if ($_FILES["images"]["error"][$i]  > 0)  {  echo "File Error : " . $_FILES["images"]["error"][$i]  . "<br />";   }   else   {   echo "Upload File Name: " . $_FILES["images"]["name"][$i]  . "<br />";  echo "File Type: " . $_FILES["images"]["type"][$i]  . "<br />";   echo "File Size: " . ($_FILES["images"]["size"][$i]  / 1024) . " Kb<br />";


   if (file_exists("public/images/".$_FILES["images"]["name"][$i] ))   {    echo "<b>".$_FILES["images"]["name"][$i]  . " already exists. </b>";    }    else   {
    move_uploaded_file($_FILES["images"]["tmp_name"][$i] ,"public/images/".             $_FILES["images"]["name"][$i] );
    echo "Stored in: " . "public/images/" . $_FILES["images"]["name"][$i] ."<br />";    ?>    Uploaded File:<br>   <img src="public/images/<?php echo $_FILES["images"]["name"][$i] ; ?>" alt="Image path    Invalid" >   <?php   }   }   }else  {   echo "Invalid file detail ::<br> file type ::".$_FILES["images"]["type"][$i] ." ,    file    size::: ".$_FILES["images"]["size"][$i] ;   }  }    }?>
1
  • 4
    Oh for heavens sake format that code so it can be read by humans Commented Jul 13, 2016 at 11:33

1 Answer 1

2

First: Please, learn to indent!

Your count is bad. You are counting the $_FILES['images']['name'] size, but you need to count how many $_FILES['images'] there are. So change your code like this (note the new position of [$i] keys):

<?php
if (isset($_POST['submit'])) {
    $count = count($_FILES["images"]);
    for ($i = 0; $i < $count; $i++) {
        if ((($_FILES["images"][$i]["type"] == "image/gif") || ($_FILES["images"][$i]["type"] == "image/jpeg") || ($_FILES["images"][$i]["type"] == "image/pjpeg")) && ($_FILES["images"][$i]["size"] < 100000)) {
            if ($_FILES["images"][$i]["error"] > 0) {
                echo "File Error : " . $_FILES["images"][$i]["error"] . "<br />";
            } else {
                echo "Upload File Name: " . $_FILES["images"][$i]["name"] . "<br />";
                echo "File Type: " . $_FILES["images"][$i]["type"] . "<br />";
                echo "File Size: " . ($_FILES["images"][$i]["size"] / 1024) . " Kb<br />";
                if (file_exists("public/images/" . $_FILES["images"][$i]["name"])) {
                    echo "<b>" . $_FILES["images"][$i]["name"] . " already exists. </b>";
                } else {
                    move_uploaded_file($_FILES["images"][$i]["tmp_name"], "public/images/" . $_FILES["images"][$i]["name"]);
                    echo "Stored in: " . "public/images/" . $_FILES["images"][$i]["name"] . "<br />";
                    ?>    Uploaded File:
                    <br>   
                    <img src="public/images/
                    <?php echo $_FILES["images"][$i]["name"]; ?>" alt="Image path    Invalid" >   
                <?php
                }
            }
        } else {
            echo "Invalid file detail ::<br> file type ::" . $_FILES["images"][$i]["type"] . " ,    file    size::: " . $_FILES["images"][$i]["size"];
        }
    }
}?>

This code works assuming your html is something like this:

<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.