美文网首页
PHP文件上传错误代码,状态测试,调试

PHP文件上传错误代码,状态测试,调试

作者: LeeBoot | 来源:发表于2017-07-31 14:13 被阅读0次
本文主题,处理文件上传时遇到的各种问题
思想:利用超全局变量$_FILES所带的['error']状态码确定问题
关于本文的一些关键字的定义,一定要注意了!!!
  • $_FILES['userfile']['error']
    userfiel:上传文件时input表单name
    error:这个是$_FILES自带的。不用管;下面的程序会给你说明这是个什么东西
  • move_uploaded_file():
    此函数仅用于通过 HTTP POST 上传的文件。
    如果目标文件已经存在,将会被覆盖。
$code = $_FILES['userfile']['error'];
if( $code > 0) { //判断文件是否成功上传到服务器,0表示上传成功
    echo 'Error: '.$code;

    switch ( $code  ) {
        case UPLOAD_ERR_OK:
            //0:没有错误,上传成功的文件。
            $response = 'There is no error, the file uploaded with success.';
            break;
        case UPLOAD_ERR_INI_SIZE:
            //1:上传文件在php.ini中超过upload_max_filesize指令
            $response = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
            break;
        case UPLOAD_ERR_FORM_SIZE:
            //2:上传的文件超过了在HTML表单中指定的max_file_size指令。
            $response = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
            break;
        case UPLOAD_ERR_PARTIAL:
            //3:上传的文件只是部分上传。
            $response = 'The uploaded file was only partially uploaded.';
            break;
        case UPLOAD_ERR_NO_FILE:
            //4:没有上传文件。
            $response = 'No file was uploaded.';
            break;
        case UPLOAD_ERR_NO_TMP_DIR:
            //6:缺少一个临时文件夹中。在PHP 4.3.10和PHP 5.0.3中引入。
            $response = 'Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.';
            break;
        case UPLOAD_ERR_CANT_WRITE:
            //7:未能将文件写入磁盘。
            $response = 'Failed to write file to disk. Introduced in PHP 5.1.0.';
            break;
        case UPLOAD_ERR_EXTENSION:
            //8:PHP扩展停止了文件上传。PHP没有提供一种方法来确定是哪个扩展导致了文件上载停止
            $response = 'A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop;';
            break;
        default:
            //未知的错误
            $response = 'Unknown error';
            break;
    }
    echo $response;

    exit; //如果$_FILES['userfile']['error']大于0都是有错误,输出错误信息并退出程序
}else{
    echo "success".$code;
    $file = $_FILES['userfile'];
    var_dump($file);
    die;
}
  • 另外附上我的一个上传文件函数;自我BB一下:简直是好用的不要不要的。
/*图片上传,可改动文件名加密算法*/
    public function uploadFile($file, $path, $allow_type){
        $file = $_FILES[$file];
        if($file['error'] == 0){
            $name = $file['name'];
            /*得到上传文件的类型,并且都转化成小写*/
            $file_type = strtolower(substr($name,strrpos($name,'.')+1));
            if(!in_array($file_type, $allow_type)){
                return "请上传允许的格式文件";
            }
            /*上传文件的存放路径*/
            $upload_path = $path.date('Y-m-d').'/';
            if( !file_exists($upload_path) ){
                mkdir($upload_path,0, true);
            }
            $sql_url = $upload_path.md5($name.time().rand(4,100) ).'.'.$file_type;
            if( move_uploaded_file($file['tmp_name'], $sql_url) ){
                /*将状态码返回即可,解偶。将需要进行的操作放外面*/
                return json_encode(['code'=>$file['error'],'data'=>$sql_url],JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT );
            }else{
                return json_encode(['code'=>$file['error'],'data'=>"上传失败,未知错误!"],JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT );
            }
        }else{
            return json_encode(['code'=>$file['error'],'data'=>"上传失败,错误代码:"],JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT );
        }
    }

相关文章

网友评论

      本文标题:PHP文件上传错误代码,状态测试,调试

      本文链接:https://www.haomeiwen.com/subject/qdfrlxtx.html