首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Content-type常见的值和PHP文件上传函数.

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

作者头像
老K博客
发布2023-12-18 15:52:21
发布2023-12-18 15:52:21
8350
举报
文章被收录于专栏:老K博客老K博客

Content-type常见的值

  1. application/x-www-form-urlencoded form表单的enctype的默认值
  2. multipart/form-data 如果表单中有文件或者图片之类的不能被编码的元素,浏览器可以用此方式传输数据,提高传输效果和用户体验,也可以减少服务器的请求次数.
  3. application/json JSON.stringify 此方法可以传输json数据, 跨脚本

PHP文件上传,封装多文件上传函数 上传单个文件 html

代码语言:javascript
复制
       提交

php

代码语言:javascript
复制
print_r(uploadFile($_FILES));
function uploadFile(array $files,$uploadPath='uploads'):array
{
  if(!file_exists($uploadPath)){  //判断存储的路径是否存在,不存在即创建文件夹
       mkdir($uploadPath,0777,true);  //默认权限是 0777最大可能的访问权
  }
  foreach($files as $file){
    if($file['error']==0){     //error==0表示无错误
          if(strstr($file['type'],'/',true)!=='image'){  //strstr  查找字符串中首次出现 true表示返回前面部分
            $tips = $file['name'].'文件类型错误';
            continue;
          }else{
            //生成文件名       
            $targetName = $uploadPath.'/'.date('YmdHis').md5($file['name']).time().strstr($file['name'],'.');
            // echo $targetName;  
            // die;
            //将文件从临时位置移动到指定位置
            if(!move_uploaded_file($file['tmp_name'],$targetName)){
                $tips = $file['name'].'文件移动失败';
                continue;   //循环结构用用来跳过本次循环中剩余的代码并在条件求值为真时开始执行下一次循环。
            }else{
                $img[] = $targetName;
            }
        }
    }
  }
  if(!empty($tips)){
    $res['error'] = $tips;
  }
  $res['fileRealPath'] = $img;
  return $res;
}

上传多个文件 html

代码语言:javascript
复制
   多个文件上传

php

代码语言:javascript
复制
$res = upload($_FILES);
print_r(uploadFile($res));
function uploadFile(array $files,$uploadPath='uploads/storage'):array
{
  if(!file_exists($uploadPath)){  //判断存储的路径是否存在,不存在即创建文件夹
       mkdir($uploadPath,0777,true);  //默认权限是 0777最大可能的访问权
  }
  foreach($files as $file){
    if($file['error']==0){     //error==0表示无错误
          if(strstr($file['type'],'/',true)!=='image'){  //strstr  查找字符串中首次出现 true表示返回前面部分
            $tips = $file['name'].'文件类型错误';
            continue;
          }else{
            //生成文件名       
            $targetName = $uploadPath.'/'.date('YmdHis').md5($file['name']).time().strstr($file['name'],'.');
            // echo $targetName;  
            // die;
            //将文件从临时位置移动到指定位置
            if(!move_uploaded_file($file['tmp_name'],$targetName)){
                $tips = $file['name'].'文件移动失败';
                continue;   //循环结构用用来跳过本次循环中剩余的代码并在条件求值为真时开始执行下一次循环。
            }else{
                $img[] = $targetName;
            }
        }
    }
  }
  if(!empty($tips)){
    $res['error'] = $tips;
  }
  $res['fileRealPath'] = $img;
  return $res;
}
// 处理多文件的格式
function upload(): array
{
    $i = 0;
    foreach ($_FILES as $k => $file) {
        // printf('%s', print_r($file, true));
        foreach ($file['name'] as $k => $v) {
            $files[$i]['name'] = $file['name'][$k];
            $files[$i]['type'] = $file['type'][$k];
            $files[$i]['tmp_name'] = $file['tmp_name'][$k];
            $files[$i]['error'] = $file['error'][$k];
            $files[$i]['size'] = $file['size'][$k];
            $i++;
        }
    }
    // printf('%s', print_r($files, true));
    return $files;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023年10月26日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档