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
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");
  }
}
}

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.