PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

博客列表 > Content-type常见的值和PHP文件上传函数.

Content-type常见的值和PHP文件上传函数.

飞天001
飞天001 原创
2023年04月29日 11:44:12 582浏览

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. }
上一条: Laravel路由 下一条: 联系官方,扫码联系
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议