首页 » 后端 » PHP » 正文

PHP 文件上传中的错误代码 [转]

发布者:站点默认
2012/10/24 浏览数(7,091) 分类:PHP PHP 文件上传中的错误代码 [转]已关闭评论

错误码:

0 | UPLOAD_ERR_OK         | 文件成功上传
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    | 文件没有完整上传
4 | UPLOAD_ERR_NO_FILE    | 没有上传文件
5 | UPLOAD_ERROR_E        | As expliained by @Progman, removed in rev. 81792
6 | UPLOAD_ERR_NO_TMP_DIR | 找不到临时文件夹
7 | UPLOAD_ERR_CANT_WRITE | 磁盘不可写
8 | UPLOAD_ERR_EXTENSION  | File upload stopped by extension.

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></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>

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></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>

<完。转自:http://summerbluet.com/642>

点击返回顶部
  1. 留言
  2. 联系方式