$_FILES详解
<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']); } ?>