0

So I have this form in a for loop which submits to itself and gets images uploaded by users, the form code:

    for ($x=1; $x<=10; $x++)
    {
    echo "<form method='post' enctype='multipart/form-data'>";

    echo "
    <label for='file'>Image:</label>
    <input type='file' name='file'><br>
    <input type='submit' name='submit' value='Upload Image'>
    <br>";

    include ('lib/upload.php');
    }

the form will then call a script named "upload.php" which contains these codes:

    <?php
     if (isset($_POST['submit']))
     {
        $allowedext=array("gif", "png", "jpeg", "jpg");
        $tmp=explode(".", $_FILES["file"]["name"]);
        $ext=end($tmp);

        if (
        (($_FILES["file"]["type"]=="image/gif") ||
        ($_FILES["file"]["type"]=="image/jpeg") ||
        ($_FILES["file"]["type"]=="image/jpg") ||
        ($_FILES["file"]["type"]=="image/pjpeg") ||
        ($_FILES["file"]["type"]=="image/png") ||
        ($_FILES["file"]["type"]=="image/x-png")) &&
        ($_FILES["file"]["size"]<=3145728) && in_array($ext, $allowedext)
        )
        {
            if ($_FILES["file"]["error"]>0)
            {
                echo "<br>Error: ".$_FILES["file"]["error"]."<br>";
            }
            else
            {
                if (file_exists("upload/".$_FILES["file"]["name"]))
                {
                    echo "<br><p style='color: #ff0000;'>".$_FILES["file"]["name"]." already exists.</p><br>";
                }
                else
                {
                    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$_FILES["file"]["name"]);
                    $x="upload/".$_FILES["file"]["name"];

                    echo "<br><img src='$x' width='320px' height='240px'><br>";
                }
            }
        }
        else if ($_FILES["file"]["size"]>3145728)
        {
            echo "<br><p style='color: #ff0000;'>File too large</p><br>";
        }
        else
        {
            echo "<br><p style='color:#ff0000;'>Invalid file</p><br>";
        }
     }
    ?>

So my problem is, everytime I upload an image, the error "Invalid File" comes up even though I'm pretty sure I uploaded a valid file. This works if I loop the form once, like this:

    for ($x=1; $x<=1; $x++)
    {
        \\code
    }

any ideas what the problem could be?

0

3 Answers 3

1

I Found something wrong in you code,also something wrong in with the process of multiple file upload:

in you first code fragment there have a for loop,and this code is wrong include ('lib/upload.php'); i don't know why no error occur for you, but you should use include_onde instead include.this is what you code error.

with multiple file upload you needn't create multiple form,you should create a form like this:

<form>
   <input type='file' name='upload_file[]' />
   <input type='file' name='upload_file[]' />
   <input type='file' name='upload_file[]' />
   <input type='file' name='upload_file[]' />
</form>

and in you php something like this:

foreach ($_FILES as $key => $value)
{

    foreach($value as $k=>$v)
    {
           if($k !== "error")
           {
              $_FILES[$key][$k] = array_filter($v);
           }                 
     }
}
$name = $_FILES['upload_file']['name'];
if(empty($name))
{
  //do whatever you want to tell user no file uploaded
}

above code is for remove empty upload file form $_FILES;

then

$tmp_name=$_FILES['upload_file']['tmp_name'];
$size=$_FILES['upload_file']['size'];
$error=$_FILES['upload_file']['error'];
$filepath = "/usr/www/project/image/";
$ext="get ext by yourself";//like ".jpg" 
foreach($name as $key=>$value)
{
  move_uploaded_file($tmp_name["{$key}"], $filepath.$value.$ext);
  //do other thing here one by one.
 }

enjoy youself..

Sign up to request clarification or add additional context in comments.

Comments

0

you lost a form in your code.

insert echo '&lt;/form&gt;'; in your code.

introduce you a website: http://www.w3schools.com/PHP/php_file_upload.asp

enjoy your learning time.

Comments

0

You forgot to add </form> at the end of for loop.

for ($x=1; $x<=10; $x++)
    {
    echo "<form method='post' enctype='multipart/form-data'>";

    echo "
    <label for='file'>Image:</label>
    <input type='file' name='file'><br>
    <input type='submit' name='submit' value='Upload Image'>
    <br></form>"; //end form here

    include ('lib/upload.php');
    }

Your form is not closing so creating problem with another forms. But it get closed then all forms will work fine.

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.