Ankur Saxena
Ankur Saxena

Reputation: 639

PHP upload validation not working

I make a function of upload a file. Apply certain validation but my validation not working properly I tried but I'm unable to get the bug.

Here is my code:

function upload($file){

        $allowedExts = array("gif", "jpeg", "jpg", "png");
        $filename=$_FILES[$file]['name'];                       //file name
        $filetmp=$_FILES[$file]['tmp_name'];                    //file in php tmp folder
        $filesize=$_FILES[$file]['size'];                       //file size in bytes
        $filetype=$_FILES[$file]['type'];                       //file type
        $fileerror=$_FILES[$file]['error'];                     //0=false && 1=true
        $extension = end(explode(".",$filename));               //split the file name into array using a dot

        if ((  ($filetype == "image/gif")
            || ($filetype == "image/jpeg")
            || ($filetype == "image/jpg")
            || ($filetype == "image/pjpeg")
            || ($filetype == "image/x-png")
            || ($filetype == "image/png"))
            && ($filesize < 20000)
            && in_array($extension, $allowedExts)
            && ($fileerror==0)){
            if (file_exists("assets/upload/park_logo/upload/" . $filename)?FALSE :TRUE)
            {
                if(TRUE){
                    move_uploaded_file($filetmp,'assets/upload/park_logo/'.$filename);    
                }
            }else{
                return FALSE;
            }
        }else{
            return FALSE;
        }   
    }

Call this function: -

if(upload('logo_upload')==FALSE) {
    $error[]="please upload file size:2mb,type:png or jpeg format";
}

It shows error message and it upload successfully.

NOTE: -can we function like this way. I have two upload field can I use like this

if(upload('logo_upload'|| 'pic_upload')==FALSE) {
    $error[]="please upload file size:2mb,type:png or jpeg format";
}

Upvotes: 0

Views: 136

Answers (2)

omma2289
omma2289

Reputation: 54619

Try it like this:

function upload($file){
    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $filename=$_FILES[$file]['name'];                       //file name
    $filetmp=$_FILES[$file]['tmp_name'];                    //file in php tmp folder
    $filesize=$_FILES[$file]['size'];                       //file size in bytes
    $filetype=$_FILES[$file]['type'];                       //file type
    $fileerror=$_FILES[$file]['error'];                     //0=false && 1=true
    $extension = end(explode(".",$filename));               //split the file name into array using a dot

    if ((  ($filetype == "image/gif")
        || ($filetype == "image/jpeg")
        || ($filetype == "image/jpg")
        || ($filetype == "image/pjpeg")
        || ($filetype == "image/x-png")
        || ($filetype == "image/png"))
        && ($filesize < 20000)
        && in_array($extension, $allowedExts)
        && ($fileerror==0)){
        if (!file_exists("assets/upload/park_logo/upload/" . $filename))
        {   
            return move_uploaded_file($filetmp,'assets/upload/park_logo/'.$filename);    
        }else{
            return FALSE;
        }
    }else{
        return FALSE;
    }   
}

Upvotes: 1

n1te
n1te

Reputation: 945

Change this:

if (file_exists("assets/upload/park_logo/upload/" . $filename)?FALSE :TRUE)
{
        if(TRUE){
            move_uploaded_file($filetmp,'assets/upload/park_logo/'.$filename);    
        }
}else{
    return FALSE;
}

to this:

if (file_exists("assets/upload/park_logo/upload/" . $filename)) {
    if (move_uploaded_file($filetmp,'assets/upload/park_logo/'.$filename) {
        return true;
    } 
    else {
        return false;
    }
} 
else {
    return false;
}

Upvotes: 1

Related Questions