解决php PUT PATCH 上传文件收不到数据
2017-04-20 17:59
411 查看
class FormDataParser { private static $partSize = 4096; //每次最大获取字节 /** * 负责解析FormData */ public static function parser($options = []) { //$options['saveFile'] = true; 测试能否正常保存临时文件 $formData = fopen("php://input", "r"); $retData = []; $boundary = rtrim(fgets($formData), "\r\n"); //第一行是boundary $info = []; //info段的信息 $data = ''; //拼接的数据 $infoPart = true; //是否是info段 while ($line = fgets($formData, self::$partSize)) { if ($boundary . "\r\n" == $line || $boundary . "--\r\n" == $line) { //如果是分割 $infoPart = true; if ($info['type'] == 'json') { $data = json_decode($data, true); $retData[$info['name']] = $data; } else if($info['type'] == 'file') { if(isset($info['tmp_file'])) { fclose($info['file_handle']); $retData[$info['name']] = [ 'org_name' => $info['org_name'], 'tmp_file' => $info['tmp_file'] ]; } else { $retData[$info['name']] = $data; } } $data = ''; } else if ("\r\n" == $line) { if ($infoPart) { //解析info $info = self::parserInfo($data, $options); if (isset($info['tmp_file'])) { $info['file_handle'] = fopen($info['tmp_file'], 'w'); } $data = ''; $infoPart = false; } } else { if($infoPart == false && isset($info['tmp_file'])) { fwrite($info['file_handle'], $line); } else { $data .= $line; } } } fclose($formData); print_r($retData); } private static function parserInfo($data, $options) { //获取参数名称, type $infoPattern = '/name="(.+?)"(; )?(filename="(.+?)")?/'; //todo: 待优化 preg_match($infoPattern, $data, $matches); $info['name'] = $matches[1]; $info['type'] = 'json'; //如果是文件 if (count($matches) > 4) { $info['type'] = 'file'; $info['org_name'] = $matches[4]; //如果设置保存文件, 保存到临时文件 if (isset($options['saveFile']) && $options['saveFile']) { $tmpFile = tempnam(sys_get_temp_dir(), 'FD'); $info['tmp_file'] = $tmpFile; } } return $info; } }
put patch 用这个方法可以接受文件 和数据 ,解决表单乱码。
相关文章推荐
- httpClient4.3 模拟Post提交,模拟Post上传文件并解决服务端使用spingMVC时upload.parseRequest(request)解析不到File,Pos简单抓取页面数据
- php使用mysqldump命令导出数据 文件内容为空 的解决(密码不能为空)
- php上传文件,内存不足解决之道
- 解决 nginx 环境下的php 上传文件大小限制
- windows下使用IIS配置的PHP无法上传文件的解决方法
- 解决Apache2+PHP上传文件大小限制的问题
- php上传xls文件导入到mysql数据表
- 获取文件上传页面中文数据时乱码问题的解决办法
- APACHE FTP 文件上传丢失数据的解决方法
- php 无法上传文件的 一个解决可能
- IIS7.5+PHP文件上传失败的解决
- 解决php无法上传大文件问题
- php 无法上传文件的 一个解决可能
- 解决RHAS3中Apache2的PHP上传文件大小的限制
- php上传xls文件导入到mysql数据表
- PHP中完美解决fckeditor上传中文文件与新建中文目录出现乱码的问题
- php上传文件大小限制问题的解决
- 通过设置nginx的client_max_body_size解决nginx+php上传大文件的问题
- PHP学习之路之PHP文件上传需要考虑的问题以及解决方法($_FILES,$_SERVER,substr,strrpos,file_exists,move_uploaded_file,is_uploaded_file,mkdir)
- 设置tomcat虚拟内存大小,解决上传数据量大的文件,报内存溢出的问题。