如何封装PHP中的文件上传功能函数?

2026-05-06 07:271阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计620个文字,预计阅读时间需要3分钟。

如何封装PHP中的文件上传功能函数?

PHP封装PHP文件上传函数示例:

function uploadFile($fileInfo, $uploadPath='./attachment', $flag=true, $allowExt=['jpg', 'png', 'wbmp', 'gif', 'jpeg', 'bmp'], $maxSize=2097152) { if ($fileInfo['error']==0) { $arr=explode('.', $fileInfo['name']); // 更多代码... }}

如何封装PHP中的文件上传功能函数?

PHP文件上传函数封装

  1. <?php
  2. function uploadFile( $fileInfo, $uploadPath = './attachment', $flag = true, $allowExt = ['jpg', 'png', 'wbmp', 'gif', 'jpeg', 'bmp'], $maxSize = 2097152 ) {
  3. if ( $fileInfo['error'] == 0 ) {
  4. $arr = explode( '.', $fileInfo['name'] );
  5. //explode 分割字符串,以.点分割
  6. $ext = end( $arr );
  7. //取后缀名
  8. $prefix = array_shift( $arr );
  9. //取文件名
  10. if ( !in_array( $ext, $allowExt ) ) {
  11. //判断后缀是否存在
  12. return $res = '文件类型不合法';
  13. }
  14. if ( $fileInfo['size'] > $maxSize ) {
  15. //判断文件大小
  16. return $res = '文件大小超过限制的最大值';
  17. }
  18. if ( !getimageSize( $fileInfo['tmp_name'] ) ) {
  19. //检测图片是否合法
  20. return $res = '请勿上传非法图片';
  21. }
  22. if ( !is_uploaded_file( $fileInfo['tmp_name'] ) ) {
  23. //检测上传方式是否为img.558idc.com/uploadfile.php"
  24. method="post"
  25. enctype="multipart/form-data"
  26. >
  27. <fieldset>
  28. <legend>文件上传</legend>
  29. <label for="my_file"></label>
  30. <input type="file" name="my_file" id="my_file" />
  31. <button>上传</button>
  32. </fieldset>
  33. </form>
  34. </body>
  35. </html>

成功上传文件,并按年/月分类存放

本文共计620个文字,预计阅读时间需要3分钟。

如何封装PHP中的文件上传功能函数?

PHP封装PHP文件上传函数示例:

function uploadFile($fileInfo, $uploadPath='./attachment', $flag=true, $allowExt=['jpg', 'png', 'wbmp', 'gif', 'jpeg', 'bmp'], $maxSize=2097152) { if ($fileInfo['error']==0) { $arr=explode('.', $fileInfo['name']); // 更多代码... }}

如何封装PHP中的文件上传功能函数?

PHP文件上传函数封装

  1. <?php
  2. function uploadFile( $fileInfo, $uploadPath = './attachment', $flag = true, $allowExt = ['jpg', 'png', 'wbmp', 'gif', 'jpeg', 'bmp'], $maxSize = 2097152 ) {
  3. if ( $fileInfo['error'] == 0 ) {
  4. $arr = explode( '.', $fileInfo['name'] );
  5. //explode 分割字符串,以.点分割
  6. $ext = end( $arr );
  7. //取后缀名
  8. $prefix = array_shift( $arr );
  9. //取文件名
  10. if ( !in_array( $ext, $allowExt ) ) {
  11. //判断后缀是否存在
  12. return $res = '文件类型不合法';
  13. }
  14. if ( $fileInfo['size'] > $maxSize ) {
  15. //判断文件大小
  16. return $res = '文件大小超过限制的最大值';
  17. }
  18. if ( !getimageSize( $fileInfo['tmp_name'] ) ) {
  19. //检测图片是否合法
  20. return $res = '请勿上传非法图片';
  21. }
  22. if ( !is_uploaded_file( $fileInfo['tmp_name'] ) ) {
  23. //检测上传方式是否为img.558idc.com/uploadfile.php"
  24. method="post"
  25. enctype="multipart/form-data"
  26. >
  27. <fieldset>
  28. <legend>文件上传</legend>
  29. <label for="my_file"></label>
  30. <input type="file" name="my_file" id="my_file" />
  31. <button>上传</button>
  32. </fieldset>
  33. </form>
  34. </body>
  35. </html>

成功上传文件,并按年/月分类存放