0

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

这是我的代码:

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

2 回答 2

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;
}
于 2013-06-29T07:34:41.220 回答
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;
    }   
}
于 2013-06-29T07:44:13.183 回答