文件上传


上传单张图片,以上传一个商品封面为例

html页面代码

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="utf-8">
  5. </head>
  6. <body>
  7. <form method="post" enctype="multipart/form-data">
  8. <p><input type="file" name="cover" /></p>
  9. <p><input type="submit" value="上传" /></p>
  10. </form>
  11. </body>

PHP代码

  1. class GoodsModel extends Model
  2. {
  3. public function publishGoods()
  4. {
  5. $data = array();
  6. $goods_id = $this->insert($data);
  7. //上传封面图
  8. $file = $this->uploadGoodsCover($goods_id);
  9. print_r($file);
  10. }
  11. protected function uploadGoodsCover($goods_id)
  12. {
  13. $upload = new UploadFiles();
  14. $four_path = Helper::getFourPath($goods_id); // 000/00/10/09/
  15. $relative_path = 'goods/cover/' . $four_path; // goods/cover/000/00/01/00/
  16. $file_dir = Loader::getFilePath('static::' . $relative_path);
  17. $ret = $upload->upload('cover', $file_dir);
  18. if (!$ret) {
  19. return false;
  20. }
  21. $file = $upload->getSuccessInfo()[0];
  22. return $file;
  23. }
  24. }
  25. (new GoodsModel())->publishGoods();
  1. 输出数据如下:
  2. Array
  3. (
  4. [name] => 57d7900d264b9.jpg
  5. [type] => image/jpeg
  6. [tmp_name] => C:\Windows\Temp\phpBA9C.tmp
  7. [error] => 0
  8. [size] => 31051
  9. [extension] => jpg
  10. [savepath] => E:\store\TimoPHP\wwwroot\web\static\goods\cover\000\00\01\09\
  11. [savename] => 5809b14e2fa5c1486.jpg
  12. )

上传多张图片

html页面代码

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="utf-8">
  5. </head>
  6. <body>
  7. <form method="post" enctype="multipart/form-data">
  8. <p><input type="file" name="cover[]" /></p>
  9. <p><input type="file" name="cover[]" /></p>
  10. <p><input type="file" name="cover[]" /></p>
  11. <p><input type="submit" value="上传" /></p>
  12. </form>
  13. </body>

PHP代码

  1. $upload = new UploadFiles();
  2. $ret = $upload->upload('cover', $file_dir);
  3. if (!$ret) {
  4. return false;
  5. }
  6. $files = $upload->getSuccessInfo()
  7. print_r($files);

输出如下:

  1. Array
  2. (
  3. [0] => Array
  4. (
  5. [name] => 57d7900d264b9.jpg
  6. [type] => image/jpeg
  7. [tmp_name] => C:\Windows\Temp\phpBA9C.tmp
  8. [error] => 0
  9. [size] => 31051
  10. [extension] => jpg
  11. [savepath] => E:\store\TimoPHP\wwwroot\web\static\homework\000\00\01\99\
  12. [savename] => 5809b14e2fa5c1486.jpg
  13. )
  14. [1] => Array
  15. (
  16. [name] => 01191439710123.jpg
  17. [type] => image/jpeg
  18. [tmp_name] => C:\Windows\Temp\phpBAAD.tmp
  19. [error] => 0
  20. [size] => 30127
  21. [extension] => jpg
  22. [savepath] => E:\store\TimoPHP\wwwroot\web\static\homework\000\00\01\99\
  23. [savename] => 5809b14e2fe448387.jpg
  24. )
  25. [2] => Array
  26. (
  27. [name] => 20160929123459.png
  28. [type] => image/png
  29. [tmp_name] => C:\Windows\Temp\phpBAAE.tmp
  30. [error] => 0
  31. [size] => 18165
  32. [extension] => png
  33. [savepath] => E:\store\TimoPHP\wwwroot\web\static\homework\000\00\01\99\
  34. [savename] => 5809b14e3022c4328.png
  35. )
  36. )