0

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";
}
8
  • What is the error message ? Commented Jun 29, 2013 at 7:04
  • The if(TRUE){ is meaningless. It will always run, so you do not need it. The ternary if(file_exists(...)?FALSE:TRUE) is also unnecessary. Commented Jun 29, 2013 at 7:12
  • @K Abhishek:-please upload file size:2mb,type:png or jpeg format Commented Jun 29, 2013 at 7:13
  • @ Sverri M. Olsen:-i tried but getting the error please clear your reply so that i will sort out my problem.thank Commented Jun 29, 2013 at 7:14
  • same goes for this file_exists("assets/upload/park_logo/upload/" . $filename)?FALSE :TRUE since file_exists() already returns boolean Commented Jun 29, 2013 at 7:14

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

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.