PHP一步步实现文件上传及上传文件类

一,比较简单的实现文件上传

文件上传原理:文件上传原理:将客户端的文件先上传到服务器端,然后再将服务器端的临时文件移动到指定的目录。

客户端配置:要上传文件,我们需要采用表单,并且表单发送的形式来POST请求,而且要求将enctype设置为multipart/form-data,总结上传的条件如下:

浏览器表单页面:表单发送方式为post,指定enctype=multipart/form-data,客户端的代码:

  1. <form action="uploadFile.php" method="post" accept-charset="utf-8" enctype="multipart/form-data">
  2. 请选择要上传的文件:
  3. <input type="file" name="myfile">
  4. <input type="submit" value="上传文件">
  5. </form>

$_FILES文件变量,下面是上传一个图片然后打印整理出来的数据:

  1. // name => 'QC8054R7QPGQ_1000x500.jpg'
  2. // type => 'image/jpeg'
  3. // tmp_name => '/Applications/MAMP/tmp/php/php1X5KZU'
  4. // error => 0
  5. // size => 229936

$_FILES上传的参数含义说明:

name:上传的文件的名称

type: 上传的文件的MIME类型

tmp_name:文件上传到服务器的临时路径

site:上传的文件的大小

error:上传的文件的错误码,0表示上传成功UPLOAD_ERR_OK

移动文件

移动文件方式一:上传文件到服务器端是在一个临时路径下,我们需要将文件移动到指定的目录下,我们可以通过下面的函数来实现移动:将指定的文件移到的目录路径下,要求待移动的文件是通过HTTP POST上传的.

bool move_uploaded_file ( string $filename , string $destination )

我们需要判断一下是否是通过HTTP POST上传的,下面的方法可以判断:判断文件是否是通过HTTP POST上传的,如果是返回TRUE,否则返回FALSE

bool is_uploaded_file ( string $filename )

移动文件方式二:

我们还可以通过下面的函数来实现移动文件:

参数一:待移动的文件

参数二:移动到的目标路径

bool copy ( string $source , string $dest [, resource $context ] )

处理上传:

  1. define('UPLOAD_PATH', 'Uploads');
  2. $name = $_FILES['myfile']['name'];
  3. $type = $_FILES['myfile']['type'];
  4. $tmp_name = $_FILES['myfile']['tmp_name'];
  5. $error = $_FILES['myfile']['error'];
  6. $size = $_FILES['myfile']['size'];
  7. if ($error == UPLOAD_ERR_OK) {
  8. if (is_uploaded_file($tmp_name)) {
  9. move_uploaded_file($tmp_name, UPLOAD_PATH . '/' . $name);
  10. } else {
  11. if (is_file($tmp_name) && !copy($tmp_name, UPLOAD_PATH . '/' . $name)) {
  12. var_dump('ok');
  13. } //phpfensi.com
  14. }
  15. } else {
  16. // 上传到服务器就已经出错了
  17. var_dump($error);
  18. }

php.ini上传配置:

假设我们要支持上传20M的文件,那么我们可以设置以下选项:

一定要设置为On,才能上传文件,若设置为Off,则服务器是接收不到文件数据的

file_uploads = On

指定上传文件到服务器的临时目录,默认为不打开的,可以不写

upload_tmp_dir = "d:/uploads_tmp"

支持上传的文件的最大为20M

upload_max_filesize = 20M

设置POST请求允许一次请求的最大值为100M

post_max_size = 100M

上传操作允许的最长时间,超过600秒则会停止脚本运行,0表示无限制

max_execution_time = 600

PHP脚本解析请求数据所用的最大时间,默认为60秒,0表示无限制

max_input_time = 600

单个PHP脚本所能申请的最大内存,-1表示无限制!

memory_limit = 128M

上传文件错误码:

UPLOAD_ERR_OK:代表上传成功

UPLOAD_ERR_EXTENSION:上传的文件被PHP扩展程序中断

UPLOAD_ERR_PARTIAL:文件只有部分被上传

UPLOAD_ERR_CANT_WRITE:文件写入失败

UPLOAD_ERR_FORM_SIZE:表单文件超过了post_max_size

UPLOAD_ERR_INI_SIZE:文件大小超过了限制上传的大小

UPLOAD_ERR_NO_FILE:没有文件被上传

UPLOAD_ERR_NO_TMP_DIR:找不到临时目录

客户端限制上传:

我们可以通过隐藏域来实现限制上传的文件大小,同时可以通过accept来限制上传的文件的类型,如下所示:

  1. <form action="uploadFile.php" method="post" accept-charset="utf-8" enctype="multipart/form-data">
  2. <input type="hidden" name="MAX_FILE_SIZE" value="1024">
  3. 请选择要上传的文件:
  4. <input type="file" name="myfile" accept="image/png">
  5. <input type="submit" value="上传文件">
  6. </form>

服务端限制上传:

我们可以通过在服务端来判断文件类型、文件大小,上传方式等来判断是否满足条件,然后才处理文件!

  1. define('UPLOAD_PATH', 'Uploads');
  2. define('MAX_FILE_SIZE', 2 * 1024 * 1024);
  3. header('Content-type:text/html;Charset=utf-8');
  4. $name = $_FILES['myfile']['name'];
  5. $type = $_FILES['myfile']['type'];
  6. $tmp_name = $_FILES['myfile']['tmp_name'];
  7. $error = $_FILES['myfile']['error'];
  8. $size = $_FILES['myfile']['size'];
  9. $allowExt = array('png', 'jpg', 'jpeg');
  10. if ($error == UPLOAD_ERR_OK) {
  11. if ($size > MAX_FILE_SIZE) {
  12. exit('上传的文件过大');
  13. }
  14. // 取上传的文件的扩展类型
  15. $ext = pathinfo($name, PATHINFO_EXTENSION);
  16. if (!in_array($ext, $allowExt)) {
  17. exit('非法文件类型');
  18. }
  19. if (!is_uploaded_file($tmp_name)) {
  20. exit('文件不是HTTP POST上传过来的');
  21. }
  22. if (move_uploaded_file($tmp_name, UPLOAD_PATH . '/' . $name)) {
  23. echo '文件上传成功';
  24. } else {
  25. echo "文件上传失败";
  26. }
  27. } else {
  28. // 上传到服务器就已经出错了
  29. var_dump($error);
  30. }

忽略文件重名之类的问题,那些需要额外添加一些小处理哦!

二,上传文件类

  1. ini_set('display_errors', 'On');
  2. error_reporting(E_ALL);
  3. header('Content-type:text/html;Charset=utf-8');
  4. /**
  5. * Class for Uploading a single image
  6. */
  7. class Upload {
  8. protected $fileName; /* eg, $_FILES['file'], the name is file. */
  9. protected $allowExt; /* Allow extension for uploading a file */
  10. protected $allowMIMEType; /* Allow uploading file mine types */
  11. protected $fileMaxSize; /* Limit a uploading file size */
  12. protected $uploadPath; /* The destination path */
  13. protected $isImageFlag; /* Note that file is an image or not. */
  14. protected $errorMessage;
  15. protected $fileExt;
  16. protected $fileInfos;
  17. protected $fileUniqueName;
  18. protected $fileDestPath;
  19. public function __construct($fileName = 'file', $uploadPath = './Uploads', $isImageFlag = true, $fileMaxSize = 1048576, $allowExt = array('png', 'jpg', 'jpeg', 'gif'), $allowMIMEType = array('image/png', 'image/jpeg', 'image/gif')) {
  20. $this->fileName = $fileName;
  21. $this->allowExt = $allowExt;
  22. $this->allowMIMEType = $allowMIMEType;
  23. $this->uploadPath = $uploadPath;
  24. $this->isImageFlag = $isImageFlag;
  25. $this->fileMaxSize = $fileMaxSize;
  26. // print_r($_FILES);
  27. $this->fileInfos = $_FILES[$fileName];
  28. }
  29. public function uploadFile() {
  30. if ($this->isValidExt()
  31. && $this->isValidMIMEType()
  32. && $this->isValidFileSize()
  33. && $this->isRealImage()
  34. && $this->isHTTPPOST()
  35. && !$this->hasError()) {
  36. $this->isUploadPathExist();
  37. $this->fileUniqueName = $this->getUniqueName();
  38. $this->fileDestPath = $this->uploadPath . '/' . $this->fileUniqueName . '.' . $this->fileExt;
  39. // echo iconv('gb2312', 'UTF-8', $this->fileDestPath);
  40. if (@move_uploaded_file($this->fileInfos['tmp_name'], $this->fileDestPath)) {
  41. return $this->fileDestPath;
  42. } else {
  43. $this->errorMessage = '文件上传失败';
  44. }
  45. } else {
  46. $this->errorMessage = '文件上传失败';
  47. }
  48. exit('<span >'.$this->errorMessage.'</span>');
  49. }
  50. protected function hasError() {
  51. $ret = true;
  52. if (!is_null($this->fileInfos)) {
  53. switch ($this->fileInfos['error']) {
  54. case UPLOAD_ERR_INI_SIZE:
  55. $this->errorMessage = '文件大小超过PHP.ini文件中upload_max_filesize';
  56. break;
  57. case UPLOAD_ERR_FORM_SIZE:
  58. $this->errorMessage = '文件大小超过了表单中MAX_FILE_SIZE设置的值';
  59. break;
  60. case UPLOAD_ERR_NO_TMP_DIR:
  61. $this->errorMessage = '找不到临时文件目录';
  62. break;
  63. case UPLOAD_ERR_NO_FILE:
  64. $this->errorMessage = '没有选择任何文件上传';
  65. break;
  66. case UPLOAD_ERR_CANT_WRITE:
  67. $this->errorMessage = '文件不可写';
  68. break;
  69. case UPLOAD_ERR_PARTIAL:
  70. $this->errorMessage = '只有部分文件被上传';
  71. break;
  72. case UPLOAD_ERR_EXTENSION:
  73. $this->errorMessage = '文件上传过程中被PHP扩展程序中断';
  74. break;
  75. default:
  76. $this->errorMessage = '';
  77. $ret = false;
  78. }
  79. } else {
  80. $this->errorMessage = '文件上传出错';
  81. }
  82. return $ret;
  83. }
  84. protected function isValidFileSize() {
  85. if ($this->fileInfos['size'] > $this->fileMaxSize) {
  86. $this->errorMessage = '文件太大';
  87. return false;
  88. }
  89. return true;
  90. }
  91. protected function isValidExt() {
  92. $ext = pathinfo($this->fileInfos['name'], PATHINFO_EXTENSION);
  93. if (!in_array($ext, $this->allowExt)) {
  94. $this->errorMessage = '不支持的文件类型';
  95. return false;
  96. }
  97. $this->fileExt = $ext;
  98. return true;;
  99. }
  100. protected function isValidMIMEType() {
  101. $type = $this->fileInfos['type'];
  102. if (!in_array($type, $this->allowMIMEType)) {
  103. $this->errorMessage = '不支持的文件MIME类型';
  104. return false;
  105. }
  106. return true;
  107. }
  108. protected function isHTTPPOST() {
  109. if (!is_uploaded_file($this->fileInfos['tmp_name'])) {
  110. $this->errorMessage = '文件不是通过HTTP POST传上来的';
  111. return false;
  112. }
  113. return true;
  114. }
  115. protected function isRealImage() {
  116. if ($this->isImageFlag && !getimagesize($this->fileInfos['tmp_name'])) {
  117. $this->errorMessage = '文件不是图片';
  118. return false;
  119. }
  120. return true;
  121. }
  122. protected function isUploadPathExist() {
  123. if (!file_exists($this->uploadPath)) {
  124. mkdir($this->uploadPath, 0777, true);
  125. }
  126. }
  127. protected function getUniqueName() {
  128. return md5(microtime(true), true);
  129. }
  130. }
  131. $upload = new Upload('myfile');
  132. if ($upload->uploadFile()) {
  133. echo "文件上传成功";
  134. }