views:

36

answers:

1

Here is the code i made, im expecting it to work but somewhere there must be an error. I can't figure out myself, Please help.

<?php

if(isset($_POST['submit'])){

    $max_size = 500000;
    $image_upload_path = "images/products/";
    $allowed_image_extension = array('jpg','jpeg','png','gif');


    for($i=0;$i<2;$i++)
    {
         //check if there is file 
        if((!empty($_FILES['image[]'][$i])) && ($_FILES['image[]']['error'][$i]==0))
        {
            //check extension
            $extension = strrchr($_FILES['image']['name'][$i], '.');
            if(in_array($extension,$allowed_image_extension))
            {
                //check file size.
                if($_FILES['image']['size'][$i] > $max_size)
                {
                    echo "file too big";
                }
                else if($_FILES['image']['size'][$i] < 1)
                {
                    echo "file empty";
                }
                else
                {
                    //we have pass file empty check,file extension check,file size check.
                    $the_uploaded_image = $_FILES['image']['tmp_name'][$i];
                    $the_uploaded_image_name = $_FILES['image']['name'][$i];

                    //replace empty space in filename with an underscore '_'
                    $the_uploaded_image_name = preg_replace('/\s/','_',$the_uploaded_image_name);

                    //get the file extension
                    $the_uploaded_image_extension = explode(',',$the_uploaded_image_name);
                    $the_new_image_name = $the_uploaded_image_name."".md5(uniqid(rand(),true))."".$the_uploaded_image_extension;

                    $save_image_as = $the_new_image_name;
                    //check file exist
                    if(file_exists($image_upload_path."".$the_new_image_name))
                    {
                        echo "file".$image_upload_path."".$the_new_image_name." already exist";
                    }
                    else
                    {
                        if(move_uploaded_file($the_uploaded_image,$save_image_as))
                        {
                            echo "image".$the_uploaded_image_name." uploaded sucessfully";
                            //set the image  path to save in database column
                        }
                        else
                        {
                            echo "there was an error uploading your image.";
                        }
                    }
                }
            }
            else
            {
            echo "extension not allowed";
            }
        }
        else
        {
            echo "please choose file to upload";
        }
    }


}

?>

<html>
<head><title>image upload</title></head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="image[]"/>
    <input type="file" name="image[]"/>
    <input type="submit" value="submit"/>
</form>
</body>
</html>

This is my new PHP code . Im getting both the result as found found not found not found. Will someone tell me what am i doing wrong here. The if else condition is seems to be not working as both the conditions are giving ouput. Why?

<?php
if(isset($_POST["submit"])) {
    echo $_POST["submit"];
    echo "<br/>";
    for($i=0;$i<count($_FILES['image'])-1;$i++)
    {
        if(!empty($_FILES['image']['tmp_name'][$i]))
        {
            echo "found";    
            echo "<br/>";
        }
        else
        {
            echo "not found";
            echo "<br/>";
        }
    }

}
else
{
    echo "form is not posted";
}

?>

+3  A: 

I guess the obvious WTF would be $_FILES['image[]'][$i], which should just be $_FILES['image'][$i] (the [] in the name makes it an array, it's not part of the name).

I'm unwilling to troubleshoot anything beyond this for you without more information. Try this at various points in the code:

echo '<pre>';
var_dump($_POST); // or other variables
echo '</pre>';

This should help you to debug your own code, something you must learn to do.

deceze
im still getting blank screen, without any error,neither the image from the input files are uploaded.
Roger Rt
@Roger then turn error reporting on, and have the script dump `$_FILES`.
Pekka
Also @Roger this really is a case for basic debugging, something nobody here can (or will) do for you. First you turn on error reporting. Then you check whether `$_POST["submit"]` is set - e.g. by making a test output `echo "submit is set!";`. Then you test whether the conditions in line 13 actually are ever fulfilled, e.g. by making another test output. And so on and so on. Everyone knowledgeable here had to go through this - it is the way we all learn
Pekka
Thank you guys for pointing out ways for debugging code. I just started learning PHP myself this week.
Roger Rt