if(isset()) check is not working properly...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

if(isset()) check is not working properly...

Post by aditya2071990 »

Please have a look at the below code, I've explained what I've done after the code...

Code: Select all

 
        //begin image upload form
        echo '
            <br /><br />
            <form method="post" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data" id="imagesForm">    
                <fieldset>
                    <legend>
                    Upload your images:
                    </legend>
                    <label>
                    Image 1:
                    <input type="file" name="image1" />
                    </label>
                    <br />
                    <label>
                    Image 2:
                    <input type="file" name="image2" />
                    </label>
                    <br />
                    <label>
                    Image 3:
                    <input type="file" name="image3" />
                    </label>
                    <br />
                    <label>
                    Image 4:
                    <input type="file" name="image4" />
                    </label>
                    <br />
                    <label>
                    Image 5:
                    <input type="file" name="image5" />
                    </label>
                    <br />
                    <br />
                    <input type="submit" name="imageSubmit" value="Upload the images>>" />
                </fieldset>
            </form> 
        ';
        
        $imageSubmitBtn=$_POST['imageSubmit'];
        //start the processing for the image upload form
        if(isset($imageSubmitBtn)){
            //assign the various values required
            //check if any of the fields has values set to them, and if they do, then start the image control function
            if(isset($_FILES['image1']['type'])){
                imgControl($_FILES['image1'], $uniqId, $userName);
            }
            if(isset($_FILES['image2']['type'])){
                imgControl($_FILES['image2'], $uniqId, $userName);
            }
            if(isset($_FILES['image3']['type'])){
                imgControl($_FILES['image3'], $uniqId, $userName);
            }
            if(isset($_FILES['image4']['type'])){
                imgControl($_FILES['image4'], $uniqId, $userName);
            }
            if(isset($_FILES['image5']['type'])){
                imgControl($_FILES['image5'], $uniqId, $userName);
            }
        }
 
    //function that handles images, takes as argument the array $_FILES['name_of_image'] and the unique id of the concerned user
    function imgControl($imgHolder, $uniqUserId, $userName){
        //get time right now
        $time=microtime();
        //create a global variable that gives the name of the image
        global $imgAssignableName;
        //get the type of the image
        $imgType=$imgHolder['type'];
        //get the current name of the image
        $imgActualName=$imgHolder['name'];
        //translate into the type of extension that browsers understand
        $imgType=str_replace('/','',$imgType);
        $imgType=str_replace('image','',$imgType);
        $imgType=str_replace('pjpeg','jpeg',$imgType);
        $imgType=str_replace('x-png','png',$imgType);
        //make the new name of the image by adding together the username, and the unique user id and the name of the image
        $imgNewName=$uniqUserId.$time.'.'.$imgType;
        //make the assignable name by adding the path to the new name
        $imgAssignable='uploadedImages/'.$imgNewName.'';
        //copy the image onto the server
        copy($imgHolder['tmp_name'], $imgAssignable);
        //insert a record in the database telling the username and the image name
        $query1=@mysql_query("insert into userimages set id='', userName='$userName', imageName='$imgAssignable', originalName='$imgActualName'");
        //if query fails, tell me why
        if(!$query1){
            echo 'Oops there\'s been an error, please try again later. Your image has not been uploaded.<br />';
            echo mysql_error();
            unlink($imgAssignable);
        }
    }
 
Its just an image upload form, which on submitting, checks which of the values are set, and executes the image upload function for each of the fields.

But what's happening is, the function is being executed for all of the form fields, regardless of the if(isset()) check. So redundant values are getting inserted into the database... what's going wrong? Please help me...
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: if(isset()) check is not working properly...

Post by Syntac »

That's because in your code, you have set $imageSubmitBtn. Change "if(isset($imageSubmitBtn))" to "if(isset($_POST["imageSubmit"]))" and everything should work fine.
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: if(isset()) check is not working properly...

Post by aditya2071990 »

Actually, that part is working fine. What was not working were those if conditions that were checking if(isset($_FILES['image1']['type'])) and so on. But your statement gave me an insight... I changed isset() to !empty(), and lo! It works!!! Thank you so much! :D
Post Reply