PHP 文件上传错误代码及实例

发布时间:2020-07-26编辑:脚本学堂
本文介绍下,php中文件上传时错误代码的对应意思及实例,有需要的朋友,可以参考下。

php文件上传的错误代码,包括如下几种:

0 | UPLOAD_ERR_OK         | There is no error, the file uploaded with success.
1 | UPLOAD_ERR_INI_SIZE   | Size exceeds upload_max_filesize in php.ini.
2 | UPLOAD_ERR_FORM_SIZE  | Size exceeds MAX_FILE_SIZE specified in HTML form.
3 | UPLOAD_ERR_PARTIAL    | The uploaded file was only partially uploaded.
4 | UPLOAD_ERR_NO_FILE    | No file was uploaded.
5 | UPLOAD_ERROR_E        | As expliained by @Progman, removed in rev.  81792
6 | UPLOAD_ERR_NO_TMP_DIR | Missing a temporary folder.
7 | UPLOAD_ERR_CANT_WRITE | Failed to write file to disk.
8 | UPLOAD_ERR_EXTENSION  | File upload stopped by extension.

1,文件上传页 index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>php文件上传-www.jb200.com</title>
</head>
<body>
<form action="upload.php"  method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
选择文件:<input type="file" name="myfile">
<input type="submit" value="上传文件">
</form>
</body>
</html>

2,接收上传数据页 upload.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <title>接收上传文件数据-www.jb200.com</title>
</head>
<body>
<?php
    if($_FILES['myfile']['error'] > 0) { //判断文件是否成功上传到服务器,0表示上传成功
        echo 'Error: ';

        switch ( $_FILES['myfile']['error']  ) {
            case UPLOAD_ERR_OK:
                $response = 'There is no error, the file uploaded with success.';
                break;
            case UPLOAD_ERR_INI_SIZE:
                $response = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
                break;
            case UPLOAD_ERR_FORM_SIZE:
                $response = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
                break;
            case UPLOAD_ERR_PARTIAL:
                $response = 'The uploaded file was only partially uploaded.';
                break;
            case UPLOAD_ERR_NO_FILE:
                $response = 'No file was uploaded.';
                break;
            case UPLOAD_ERR_NO_TMP_DIR:
                $response = 'Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.';
                break;
            case UPLOAD_ERR_CANT_WRITE:
                $response = 'Failed to write file to disk. Introduced in PHP 5.1.0.';
                break;
            case UPLOAD_ERR_EXTENSION:
                $response = 'File upload stopped by extension. Introduced in PHP 5.2.0.';
                break;
            default:
                $response = 'Unknown error';
                break;
        }
        echo $response;      
        
        exit; //如果$_FILES['myfile']['error']大于0都是有错误,输出错误信息并退出程序
    }
    
     //获取上传文件的MIME类型中的主类型和子类型
    list($maintype,$subtype)=explode("/",$_FILES['myfile']['type']);    
    
    if ($maintype=="text") {    //通过主类型限制不能上传文本文件,例如.txt .html .php等文件文件
        echo '问题: 不能上传文本文件。';
        exit; //如果用户上传文本文件则退出程序        
    }

    $upfile = './uploads/'.time().$_FILES['myfile']['name']; //定义上传后的位置和新文件名
    
    if ( is_uploaded_file($_FILES['myfile']['tmp_name']) ) { //判断是否为上传文件
        if ( !move_uploaded_file($_FILES['myfile']['tmp_name'], $upfile) ) {   //从移动文件
            echo '问题: 不能将文件移动到指定目录。';
            exit;
        }
  }else{        
     echo '问题: 上传文件不是一个合法文件: ';
     echo $_FILES['myfile']['name'];
     exit;        
    }
    echo '文件 :  '.$upfile.'  上传成功, 大小为 : ' .$_FILES['myfile']['size'].'!<br>';   //文件上传成功则输出
?>
</body>
</html>