PHP上传验证不起作用

时间:2013-06-29 07:00:33

标签: php file-upload

我创建了一个上传文件的功能。应用某些验证,但我的验证无法正常工作我尝试但我无法得到错误。

这是我的代码:

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

调用此函数: -

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

显示错误消息并成功上传。

注意: - 我们可以这样运作。我有两个上传字段,我可以像这样使用

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

2 个答案:

答案 0 :(得分:1)

改变这个:

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

到此:

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

答案 1 :(得分:1)

试试这样:

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