登录  /  注册
博主信息
博文 40
粉丝 0
评论 0
访问量 23865
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
Content-type常见的值和PHP文件上传函数.
飞天001
原创
815人浏览过

Content-type常见的值

1. application/x-www-form-urlencoded

form表单的enctype的默认值

2. multipart/form-data

如果表单中有文件或者图片之类的不能被编码的元素,浏览器可以用此方式传输数据,提高传输效果和用户体验,也可以减少服务器的请求次数.

3. application/json JSON.stringify

此方法可以传输json数据, 跨脚本

PHP文件上传,封装多文件上传函数

上传单个文件

html

  1. <form action="upload.php" method="post" enctype="multipart/form-data">
  2. <input type="file" name="my_file">
  3. <button>提交</button>
  4. </form>

php

  1. print_r(uploadFile($_FILES));
  2. function uploadFile(array $files,$uploadPath='uploads'):array
  3. {
  4. if(!file_exists($uploadPath)){ //判断存储的路径是否存在,不存在即创建文件夹
  5. mkdir($uploadPath,0777,true); //默认权限是 0777最大可能的访问权
  6. }
  7. foreach($files as $file){
  8. if($file['error']==0){ //error==0表示无错误
  9. if(strstr($file['type'],'/',true)!=='image'){ //strstr 查找字符串中首次出现 true表示返回前面部分
  10. $tips = $file['name'].'文件类型错误';
  11. continue;
  12. }else{
  13. //生成文件名
  14. $targetName = $uploadPath.'/'.date('YmdHis').md5($file['name']).time().strstr($file['name'],'.');
  15. // echo $targetName;
  16. // die;
  17. //将文件从临时位置移动到指定位置
  18. if(!move_uploaded_file($file['tmp_name'],$targetName)){
  19. $tips = $file['name'].'文件移动失败';
  20. continue; //循环结构用用来跳过本次循环中剩余的代码并在条件求值为真时开始执行下一次循环。
  21. }else{
  22. $img[] = $targetName;
  23. }
  24. }
  25. }
  26. }
  27. if(!empty($tips)){
  28. $res['error'] = $tips;
  29. }
  30. $res['fileRealPath'] = $img;
  31. return $res;
  32. }

上传多个文件

html

  1. <form action="uploads.php" method="post" enctype="multipart/form-data">
  2. <input type="file" name="my_file[]" multiple>
  3. <button>多个文件上传</button>
  4. </form>

php

  1. $res = upload($_FILES);
  2. print_r(uploadFile($res));
  3. function uploadFile(array $files,$uploadPath='uploads/storage'):array
  4. {
  5. if(!file_exists($uploadPath)){ //判断存储的路径是否存在,不存在即创建文件夹
  6. mkdir($uploadPath,0777,true); //默认权限是 0777最大可能的访问权
  7. }
  8. foreach($files as $file){
  9. if($file['error']==0){ //error==0表示无错误
  10. if(strstr($file['type'],'/',true)!=='image'){ //strstr 查找字符串中首次出现 true表示返回前面部分
  11. $tips = $file['name'].'文件类型错误';
  12. continue;
  13. }else{
  14. //生成文件名
  15. $targetName = $uploadPath.'/'.date('YmdHis').md5($file['name']).time().strstr($file['name'],'.');
  16. // echo $targetName;
  17. // die;
  18. //将文件从临时位置移动到指定位置
  19. if(!move_uploaded_file($file['tmp_name'],$targetName)){
  20. $tips = $file['name'].'文件移动失败';
  21. continue; //循环结构用用来跳过本次循环中剩余的代码并在条件求值为真时开始执行下一次循环。
  22. }else{
  23. $img[] = $targetName;
  24. }
  25. }
  26. }
  27. }
  28. if(!empty($tips)){
  29. $res['error'] = $tips;
  30. }
  31. $res['fileRealPath'] = $img;
  32. return $res;
  33. }
  34. // 处理多文件的格式
  35. function upload(): array
  36. {
  37. $i = 0;
  38. foreach ($_FILES as $k => $file) {
  39. // printf('<pre>%s</pre>', print_r($file, true));
  40. foreach ($file['name'] as $k => $v) {
  41. $files[$i]['name'] = $file['name'][$k];
  42. $files[$i]['type'] = $file['type'][$k];
  43. $files[$i]['tmp_name'] = $file['tmp_name'][$k];
  44. $files[$i]['error'] = $file['error'][$k];
  45. $files[$i]['size'] = $file['size'][$k];
  46. $i++;
  47. }
  48. }
  49. // printf('<pre>%s</pre>', print_r($files, true));
  50. return $files;
  51. }
批改老师:欧阳克欧阳克

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学