Jump to content

Form validating multiple images?


EchoFool

Recommended Posts

 

Hey,

 

 

I have a script which processes an image when it is uploaded, but now i have a new form that allows users to upload four images at a time. I store them in an array in the form like so:

 

name="file[]"

 

 

So now i am wondering how do i process each image with a forloop because using $_FILES doesn't say which image to check in the array?

 

 

Hope some one can help me!

 

 

Heres my code:

 

 

<?php


if(isset($_POST['submit'])){ echo '<p align="center">';
if ($_FILES["file"]["size"] < 1000000)
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
$filename = $_FILES["file"]["name"];
    if (file_exists("images/".$name."/".$filename))
      {
      echo "Image already uploaded!";
      }
    else
      {
if (is_dir("userimages/".$name) == FALSE){
	mkdir("images/".$name, 0777);
}



      move_uploaded_file($_FILES["file"]["tmp_name"], "userimages/".$name."/" . $filename);
  echo "Image has been uploaded!";
     }
    }
  }
else
  {
  echo "Invalid file";
  }
  }
?>

 

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/248781-form-validating-multiple-images/
Share on other sites

If u are using HTML form something like this this

 

<form method="post" enctype="multipart/form-data">
<input type="file" name="file[]" /><br>
<input type="file" name="file[]" /><br>
<input type="submit" />
</form>

 

And if you print $_FILES you will get output like

 

Array

(

    [file] => Array

        (

            [name] => Array

                (

                    [0] => ALIM5004.JPG

                    [1] => ALIM5005.JPG

                )

 

            [type] => Array

                (

                    [O] => image/jpeg

                    [1] => image/jpeg

                )

 

            [tmp_name] => Array

                (

                    [0] => D:\xampp\tmp\php7AD4.tmp

                    [1] => D:\xampp\tmp\php7AF4.tmp

                )

 

            [error] => Array

                (

                    [0] => 0

                    [1] => 0

                )

 

            => Array

                (

                    [0] => 2750396

                    [1] => 2786816

                )

 

        )

 

)

 

 

so to uload the form u can use

$files = $_FILES;

if(isset($files['error'])){
for($i=0;$i<count($files['name']);$i++){
  if($files['error'][$i] == 0 ){
     move_uploaded_file($files['tmp_name'][$i], "$uploads_dir/$name");
  }
}
}

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.