Blogger Information
Blog 30
fans 0
comment 1
visits 24074
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0219文件上传
Original
908 people have browsed it

文件上传

关于文件上传的常量与函数

常量

超全局变量$_FILES它也是以数组形式展现,主要是提现了上传文件的一些基础信息


里面键名大概意思
name文件名

type文件类型

tmp_name临时存储位置

error错误提示

size文件大小

函数

move_uploaded_file()把上传文件从临时文件夹移动到指定位置存放

  1. <?php
  2. //获取文件名
  3. $fileName = $_FILES['user_pic']['name'];
  4. //获取到后缀名
  5. $extension = pathinfo($fileName)['extension'];
  6. //临时存储地址
  7. $tmpName = $_FILES['user_pic']['tmp_name'];
  8. //指定存储地址,注意最后要拼接上后缀名,不然存储进去的没有后缀名
  9. $destName = 'upload/' . $_FILES['user_pic']['name'] . '.' . $extension;
  10. move_uploaded_file($tmpName,$destName);

is_uploaded_file()检测是否通过POST方式上传文件

  1. <?php
  2. //临时存储地址
  3. $tmpName = $_FILES['user_pic']['tmp_name'];
  4. is_uploaded_file($tmpName);

单个文件上传

HTML代码

  1. <!-- 传输方式必须是POST -->
  2. <form action="demo3.php" method="post" enctype="multipart/form-data">
  3. <fieldset>
  4. <legend>文件上传</legend>
  5. <input type="file" name="user_pic">
  6. <button>上传</button>
  7. </fieldset>
  8. </form>

PHP代码

  1. <?php
  2. // print_r($_FILES);
  3. // echo '<hr>';
  4. // echo $fileName;
  5. // echo '<hr>';
  6. // echo $extension;
  7. // echo '<hr>';
  8. // echo $tmpName;
  9. //设置错误信息提示
  10. //判断上传文件的合法性
  11. try {
  12. $error = $_FILES['user_pic']['error'];
  13. if ($error > UPLOAD_ERR_OK) {
  14. switch ($error) {
  15. case UPLOAD_ERR_INI_SIZE:
  16. throw new Exception('文件大小超过系统设置');
  17. break;
  18. case UPLOAD_ERR_FORM_SIZE:
  19. throw new Exception('文件大小超过系统设置');
  20. break;
  21. case UPLOAD_ERR_PARTIAL:
  22. throw new Exception('文件上传不完整');
  23. break;
  24. case UPLOAD_ERR_NO_FILE:
  25. throw new Exception('文件没有被上传');
  26. break;
  27. case UPLOAD_ERR_NO_TMP_DIR:
  28. throw new Exception('未找到临时文件夹');
  29. break;
  30. case UPLOAD_ERR_CANT_WRITE:
  31. throw new Exception('文件写入失败');
  32. break;
  33. default:
  34. throw new Exception('其他未知错误');
  35. }
  36. }
  37. // 获取上传文件原始名字
  38. $fileName = $_FILES['user_pic']['name'];
  39. //获取临时存储地址
  40. $tmpName = $_FILES['user_pic']['tmp_name'];
  41. //获取上传文件后缀
  42. $extension = pathinfo($fileName)['extension'];
  43. //创建文件存储地址
  44. $destName = 'upload/' . md5($fileName) . time() . '.' . $extension;
  45. //判断上传渠道的合法性
  46. //获取临时存储地址
  47. if (true === is_uploaded_file($tmpName)) {
  48. //判断文件类型
  49. if ($extension === 'pdf') {
  50. //把临时文件移动到指定文件夹存储
  51. move_uploaded_file($tmpName, $destName);
  52. echo '<script>alert("上传成功");location.href="form.html"</script>';
  53. } else {
  54. throw new Exception('文件类型错误');
  55. }
  56. } else {
  57. throw new Exception('上传方法错误');
  58. }
  59. } catch (Exception $e) {
  60. echo $e->getMessage();
  61. }

多文件上传
PHP

  1. <?php
  2. //查看获取到的数据
  3. print_r($_FILES);
  4. echo '<hr>';
  5. //把获取到的多个文件遍历出来,因为它和单个上传文件获取到的数据排列不太一样
  6. // 创建一个空数组
  7. $files = [];
  8. foreach ($_FILES as $file) {
  9. //这里判断$_FILES里面任意一个键,如果是数组就说明是多文件上传,就继续执行
  10. if (is_array($file['name'])) {
  11. for ($i = 0; $i < count($file['name']); $i++) {
  12. //这里要注意,创建的是空数组,在这里,变量前不要忘记加[],不然数据只能拿到一组
  13. $files[] = [
  14. 'name' => $file['name'][$i],
  15. 'type' => $file['type'][$i],
  16. 'tmp_name' => $file['tmp_name'][$i],
  17. 'error' => $file['error'][$i],
  18. 'size' => $file['size'][$i],
  19. ];
  20. }
  21. }
  22. }
  23. //获取到遍历后的数据,再次进行遍历,然后放进代码里面
  24. foreach ($files as $file) {
  25. try {
  26. $error = $file['error'];
  27. if ($error > UPLOAD_ERR_OK) {
  28. switch ($error) {
  29. case UPLOAD_ERR_INI_SIZE:
  30. throw new Exception('文件大小超过系统设置');
  31. break;
  32. case UPLOAD_ERR_FORM_SIZE:
  33. throw new Exception('文件大小超过系统设置');
  34. break;
  35. case UPLOAD_ERR_PARTIAL:
  36. throw new Exception('文件上传不完整');
  37. break;
  38. case UPLOAD_ERR_NO_FILE:
  39. throw new Exception('文件没有被上传');
  40. break;
  41. case UPLOAD_ERR_NO_TMP_DIR:
  42. throw new Exception('未找到临时文件夹');
  43. break;
  44. case UPLOAD_ERR_CANT_WRITE:
  45. throw new Exception('文件写入失败');
  46. break;
  47. default:
  48. throw new Exception('其他未知错误');
  49. }
  50. }
  51. // 获取上传文件原始名字
  52. $fileName = $file['name'];
  53. //获取临时存储地址
  54. $tmpName = $file['tmp_name'];
  55. //获取上传文件后缀
  56. $extension = pathinfo($fileName)['extension'];
  57. //创建文件存储地址
  58. //这里要注意,最后要把后缀名加上去,不然文件无法识别
  59. $destName = 'upload/' . md5($fileName) . time() . '.' . $extension;
  60. //创建一个数组,限定上传文件类型
  61. $update = ['jpg', 'png', 'gif', 'jpeg'];
  62. //判断上传渠道的合法性
  63. //获取临时存储地址
  64. if (true === is_uploaded_file($tmpName)) {
  65. //判断文件类型
  66. //in_array判断一个元素,是否在数组中,返回值为布尔值
  67. if (in_array($extension, $update)) {
  68. //把临时文件移动到指定文件夹存储
  69. // move_uploaded_file($tmpName, $destName);
  70. // echo '<script>alert("上传成功");location.href="form1.html"</script>';
  71. } else {
  72. throw new Exception('文件类型错误');
  73. }
  74. } else {
  75. throw new Exception('上传方法错误');
  76. }
  77. } catch (Exception $e) {
  78. echo $e->getMessage();
  79. }
  80. }

单个/多个文件上传通用类

其实就是把两个PHP文件合并到一起,然后把参数传递进去就行了

  1. <?php
  2. namespace upload;
  3. use Exception;
  4. class UpLoad_Files
  5. {
  6. public $files;
  7. public $date;
  8. public function __construct($files)
  9. {
  10. $this->files = $files;
  11. }
  12. //把$_FILES传递进去
  13. public function getFilter()
  14. {
  15. $this->date = [];
  16. foreach ($this->files as $file) {
  17. //这里判断$_FILES里面任意一个键,如果是数组就说明是多文件上传,就继续执行
  18. if (is_array($file['name'])) {
  19. for ($i = 0; $i < count($file['name']); $i++) {
  20. //这里要注意,创建的是空数组,在这里,变量前不要忘记加[],不然数据只能拿到一组
  21. $this->date[] = [
  22. 'name' => $file['name'][$i],
  23. 'type' => $file['type'][$i],
  24. 'tmp_name' => $file['tmp_name'][$i],
  25. 'error' => $file['error'][$i],
  26. 'size' => $file['size'][$i],
  27. ];
  28. }
  29. }
  30. }
  31. return $this->date;
  32. }
  33. public function getFiles()
  34. {
  35. foreach ($this->date as $file) {
  36. try {
  37. $error = $file['error'];
  38. if ($error > UPLOAD_ERR_OK) {
  39. switch ($error) {
  40. case UPLOAD_ERR_INI_SIZE:
  41. throw new Exception('文件大小超过系统设置');
  42. break;
  43. case UPLOAD_ERR_FORM_SIZE:
  44. throw new Exception('文件大小超过系统设置');
  45. break;
  46. case UPLOAD_ERR_PARTIAL:
  47. throw new Exception('文件上传不完整');
  48. break;
  49. case UPLOAD_ERR_NO_FILE:
  50. throw new Exception('文件没有被上传');
  51. break;
  52. case UPLOAD_ERR_NO_TMP_DIR:
  53. throw new Exception('未找到临时文件夹');
  54. break;
  55. case UPLOAD_ERR_CANT_WRITE:
  56. throw new Exception('文件写入失败');
  57. break;
  58. default:
  59. throw new Exception('其他未知错误');
  60. }
  61. }
  62. // 获取上传文件原始名字
  63. $fileName = $file['name'];
  64. //获取临时存储地址
  65. $tmpName = $file['tmp_name'];
  66. //获取上传文件后缀
  67. $extension = pathinfo($fileName)['extension'];
  68. //创建文件存储地址
  69. //这里要注意,最后要把后缀名加上去,不然文件无法识别
  70. $destName = 'upload/' . md5($fileName) . time() . '.' . $extension;
  71. //创建一个数组,限定上传文件类型
  72. $update = ['jpg', 'png', 'gif', 'jpeg'];
  73. //判断上传渠道的合法性
  74. //获取临时存储地址
  75. if (true === is_uploaded_file($tmpName)) {
  76. //判断文件类型
  77. //in_array判断一个元素,是否在数组中,返回值为布尔值
  78. if (in_array($extension, $update)) {
  79. //把临时文件移动到指定文件夹存储
  80. move_uploaded_file($tmpName, $destName);
  81. echo '<script>alert("上传成功");location.href="form1.html"</script>';
  82. } else {
  83. throw new Exception('文件类型错误');
  84. }
  85. } else {
  86. throw new Exception('上传方法错误');
  87. }
  88. } catch (Exception $e) {
  89. echo $e->getMessage();
  90. }
  91. }
  92. }
  93. public function getFile()
  94. {
  95. try {
  96. $error = $this->files['user_pic']['error'];
  97. if ($error > UPLOAD_ERR_OK) {
  98. switch ($error) {
  99. case UPLOAD_ERR_INI_SIZE:
  100. throw new Exception('文件大小超过系统设置');
  101. break;
  102. case UPLOAD_ERR_FORM_SIZE:
  103. throw new Exception('文件大小超过系统设置');
  104. break;
  105. case UPLOAD_ERR_PARTIAL:
  106. throw new Exception('文件上传不完整');
  107. break;
  108. case UPLOAD_ERR_NO_FILE:
  109. throw new Exception('文件没有被上传');
  110. break;
  111. case UPLOAD_ERR_NO_TMP_DIR:
  112. throw new Exception('未找到临时文件夹');
  113. break;
  114. case UPLOAD_ERR_CANT_WRITE:
  115. throw new Exception('文件写入失败');
  116. break;
  117. default:
  118. throw new Exception('其他未知错误');
  119. }
  120. }
  121. // 获取上传文件原始名字
  122. $fileName = $this->files['user_pic']['name'];
  123. //获取临时存储地址
  124. $tmpName = $this->files['user_pic']['tmp_name'];
  125. //获取上传文件后缀
  126. $extension = pathinfo($fileName)['extension'];
  127. //创建文件存储地址
  128. $destName = 'upload/' . md5($fileName) . time() . '.' . $extension;
  129. //创建一个数组,限定上传文件类型
  130. $update = ['jpg', 'png', 'gif', 'jpeg'];
  131. //判断上传渠道的合法性
  132. //获取临时存储地址
  133. if (true === is_uploaded_file($tmpName)) {
  134. //判断文件类型
  135. if (in_array($extension,$update)) {
  136. //把临时文件移动到指定文件夹存储
  137. move_uploaded_file($tmpName, $destName);
  138. echo '<script>alert("上传成功");location.href="form.html"</script>';
  139. } else {
  140. throw new Exception('文件类型错误');
  141. }
  142. } else {
  143. throw new Exception('上传方法错误');
  144. }
  145. } catch (Exception $e) {
  146. echo $e->getMessage();
  147. }
  148. }
  149. }

再把创建一个调用类,来调用他们

  1. <?php
  2. namespace upload;
  3. require 'UpLoad_Files.php';
  4. class Factory
  5. {
  6. public static $obj;
  7. //获取对象
  8. public static function getObj($files)
  9. {
  10. self::$obj = new UpLoad_Files($files);
  11. return self::$obj;
  12. }
  13. //多文件需要过滤数据
  14. public static function getFilter()
  15. {
  16. return self::$obj->getFilter();
  17. }
  18. //获取多文件上传数据
  19. public static function getFiles()
  20. {
  21. return self::$obj->getFiles();
  22. }
  23. //获取单个文件上传数据
  24. public static function getFile()
  25. {
  26. return self::$obj->getFile();
  27. }
  28. }
  29. //调用
  30. Factory::getObj($_FILES);
  31. //多文件上传需要先过滤数据
  32. Factory::getFilter();
  33. //获取多文件上传数据
  34. Factory::getFiles();
  35. //单个文件不用过滤数据,直接获取
  36. Factory::getFile();

最后,吐槽下VS,使用USE引用的时候,它有时候要自动添加一个路径,导致引用失败,不知道是不是哪里没设置对- -,被坑了30分钟去找错误,结果发现是引用的问题

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:编辑器的智能提示很方便,但不能过于依赖和相信,毕竟代码是人在写, 而不机器, 编辑器生成的代码仅供参考而已
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post