码迷,mamicode.com
首页 > 其他好文 > 详细

$_FILES详解

时间:2017-03-16 20:05:39      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:ini   获取   ipa   dex   它的   get   success   span   2.0   

<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000">
<input name="myFile" type="file">
<input type="submit" value="上传文件">
</form> 

然后upload.php中可以直接用
$_FILES
$_POST
$_GET
等函数获取表单内容。

下面针对$_FILES讲解
当客户端提交后,我们获得了一个$_FILES 数组

$_FILES数组内容如下:
$_FILES[‘myFile‘][‘name‘]      客户端文件的原名称。
$_FILES[‘myFile‘][‘type‘]       文件的MIME 类型,需要浏览器提供该信息的支持,例如"image/gif"。
$_FILES[‘myFile‘][‘size‘]        已上传文件的大小,单位为字节。
$_FILES[‘myFile‘][‘tmp_name‘]  文件被上传后在服务端储存的临时文件名,一般是系统默认。可以在php.ini的upload_tmp_dir 指定,但用 putenv() 函数设置是不起作用
$_FILES[‘myFile‘][‘error‘]      和该文件上传相关的错误代码。[‘error‘] 是在 PHP 4.2.0 版本中增加的。下面是它的说明:(它们在PHP3.0以后成了常量)

 

错误码说明:
0 UPLOAD_ERR_OK             上传成功
1 UPLOAD_ERR_INI_SIZE         超过php.ini中upload_max_filesize限制
2 UPLOAD_ERR_FORM_SIZE       超过表单中max_file_size限制
3 UPLOAD_ERR_PARTIAL          只有部分文件被上传
4 UPLOAD_ERR_NO_FILE          没有文件被上传
5                             上传文件大小是0
6 UPLOAD_ERR_NO_TMP_DIR   找不到临时文件夹
7 UPLOAD_ERR_CANT_WRITE    文件写入失败

下面是官方错误码提示代码

<?php

class UploadException extends Exception
{
    public function __construct($code) {
        $message = $this->codeToMessage($code);
        parent::__construct($message, $code);
    }

    private function codeToMessage($code)
    {
        switch ($code) {
            case UPLOAD_ERR_INI_SIZE:
                $message = "The uploaded file exceeds the upload_max_filesize directive in php.ini";
                break;
            case UPLOAD_ERR_FORM_SIZE:
                $message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
                break;
            case UPLOAD_ERR_PARTIAL:
                $message = "The uploaded file was only partially uploaded";
                break;
            case UPLOAD_ERR_NO_FILE:
                $message = "No file was uploaded";
                break;
            case UPLOAD_ERR_NO_TMP_DIR:
                $message = "Missing a temporary folder";
                break;
            case UPLOAD_ERR_CANT_WRITE:
                $message = "Failed to write file to disk";
                break;
            case UPLOAD_ERR_EXTENSION:
                $message = "File upload stopped by extension";
                break;

            default:
                $message = "Unknown upload error";
                break;
        }
        return $message;
    }
}

// Use
if ($_FILES[‘file‘][‘error‘] === UPLOAD_ERR_OK) {
//uploading successfully done
} else {
throw new UploadException($_FILES[‘file‘][‘error‘]);
}
?>

 

$_FILES详解

标签:ini   获取   ipa   dex   它的   get   success   span   2.0   

原文地址:http://www.cnblogs.com/demonxian3/p/6561227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!