理解流方式上传和form表单上传
流方式上传:
$post_input = 'php://input';
$save_path = dirname( __FILE__ );
$postdata = file_get_contents( $post_input ); if ( isset( $postdata ) && strlen( $postdata ) > 0 ) {
$filename = $save_path . '/' . uniqid() . '.jpg';
$handle = fopen( $filename, 'w+' );
fwrite( $handle, $postdata );
fclose( $handle );
if ( is_file( $filename ) ) {
echo 'Image data save successed,file:' . $filename;
exit ();
}else {
die ( 'Image upload error!' );
}
}else {
die ( 'Image data not detected!' );
}
标准表单上传:
if (!$_FILES['Filedata']) {
die ( 'Image data not detected!' );
}
if ($_FILES['Filedata']['error'] > 0) {
switch ($_FILES ['Filedata'] ['error']) {
case 1 :
$error_log = 'The file is bigger than this PHP installation allows';
break;
case 2 :
$error_log = 'The file is bigger than this form allows';
break;
case 3 :
$error_log = 'Only part of the file was uploaded';
break;
case 4 :
$error_log = 'No file was uploaded';
break;
default :
break;
}
die ( 'upload error:' . $error_log );
} else {
$img_data = $_FILES['Filedata']['tmp_name'];
$size = getimagesize($img_data);
$file_type = $size['mime'];
if (!in_array($file_type, array('image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/gif'))) {
$error_log = 'only allow jpg,png,gif';
die ( 'upload error:' . $error_log );
}
switch($file_type) {
case 'image/jpg' :
case 'image/jpeg' :
case 'image/pjpeg' :
$extension = 'jpg';
break;
case 'image/png' :
$extension = 'png';
break;
case 'image/gif' :
$extension = 'gif';
break;
}
}
if (!is_file($img_data)) {
die ( 'Image upload error!' );
}
PHP输入流php://input
在使用xml-rpc的时候,server端获取client数据,主要是通过php输入流input,而不是$_POST数组。所以,这里主要探讨php输入流php://input
对于php://input介绍,PHP官方手册文档有一段话对它进行了很明确地概述:
“php://input allows you to read raw POST data. It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives. php://input is not available with enctype=”multipart/form-data”.
翻译过来,是这样:
“php://input可以读取没有处理过的POST数据。相较于$HTTP_RAW_POST_DATA而言,它给内存带来的压力较小,并且不需要特殊的php.ini设置。php://input不能用于enctype=multipart/form-data”
我们应该怎么去理解这段概述呢?我把它划分为三部分,逐步去理解:
- 读取POST数据
- 不能用于multipart/form-data类型
- php://input VS $HTTP_RAW_POST_DATA
读取POST数据
PHPer们一定很熟悉$_POST这个内置变量。$_POST与php://input存在哪些关联与区别呢?另外,客户端向服务端交互数据,最常用的方法除了POST之外,还有GET。既然php://input作为PHP输入流,它能读取GET数据吗?这二个问题正是我们这节需要探讨的主要内容。
详见:
http://www.nowamagic.net/academy/detail/12220520
理解流方式上传和form表单上传的更多相关文章
- 巨蟒python全栈开发django11:ajax&&form表单上传文件contentType
回顾: 什么是异步? 可以开出一个线程,我发出请求,不用等待返回,可以做其他事情. 什么是同步? 同步就是,我发送出了一个请求,需要等待返回给我信息,我才可以操作其他事情. 局部刷新是什么? 通过jq ...
- django 基于form表单上传文件和基于ajax上传文件
一.基于form表单上传文件 1.html里是有一个input type="file" 和 ‘submit’的标签 2.vies.py def fileupload(request ...
- vue form表单上传文件
<script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js">< ...
- 使用form表单上传文件
在使用form表单上传文件时候,input[type='file']是必然会用的,其中有一些小坑需要避免. 1.form的 enctype="multipart/form-data" ...
- JsonResponse类的使用、form表单上传文件补充、CBV和FBV、HTML的模板语法之传值与过滤器
昨日内容回顾 Django请求生命周期 # 1.浏览器发起请求 到达Django的socket服务端(web服务网关接口) 01 wsgiref 02 uwsgi + nginx 03 WSGI协议 ...
- 在IOS设备上POST提交form表单,后台接收不到值怎么办?
原文:https://blog.csdn.net/xhaimail/article/details/90440029 最近在工作上遇到一个奇葩问题,在Android和Windows平台上做请求时参数都 ...
- 关于play!的attachments.path配置、以及关于Form表单上传请求的认识
相关链接 form表单提交multipart/form-data的请求分析:http://blog.csdn.net/five3/article/details/7181521.http://blog ...
- PHP 后台程序配置config文件,及form表单上传文件
一,配置config文件 1获取config.php文件数组, 2获取form 表单提交的值 3保存更新config.php文件,代码如下: $color=$_POST['color']; $back ...
- nodejs 模拟form表单上传文件
使用nodejs来模拟form表单进行文件上传,可以同时上传多个文件. 以前项目里有这个方法,最近在客户那里出问题了,同事说,这个方法从来就没管用过,SO,用了一天时间把这个方法给搞出来了(觉得花费的 ...
随机推荐
- Android一键锁屏APP
题记: 这个app完全是拾人牙慧,作为练手用的,其实没有什么原创的东西.当然,博客还是我自己写的,记录下来,对自己也算是一种成长吧. 转载请注明原文地址: http://www.cnblogs.com ...
- (二)VMware Harbor 安装
转自:https://blog.csdn.net/qq_33633013/article/details/82217277 一.环境.软件准备 harbor 需要依赖docker,compose工具, ...
- 7-Java-C(骰子游戏)
题目描述: 我们来玩一个游戏. 同时掷出3个普通骰子(6个面上的数字分别是1~6). 如果其中一个骰子上的数字等于另外两个的和,你就赢了. 下面的程序计算出你能获胜的精确概率(以既约分数表示) pub ...
- 5 秒创建 k8s 集群[转]
据说 Google 的数据中心里运行着超过 20 亿个容器,而且 Google 十年前就开始使用容器技术. 最初,Google 开发了一个叫 Borg 的系统(现在命令为 Omega)来调度如此庞大数 ...
- vue 数据没有驱动视图?
Part.1 问题 数据改变,视图却没有根据数据而改变. 原因在于,数据并不在 vue 监听范围之内,vue 只对事先在 data 中声明的变量丶对象等类型数据进行监听 Part.2 例子 < ...
- 搜索 || DFS || UOJ 146 信息传递
DFS+回溯 找最小环 每个人知道自己的生日,每次把自己知道的生日告诉固定的一个人,问最少多少次之后能从别人口中听到自己的生日 找一个最小环 #include <iostream> #in ...
- 最短路 || POJ 1847 Tram
POJ 1847 最短路 每个点都有初始指向,问从起点到终点最少要改变多少次点的指向 *初始指向的那条边长度为0,其他的长度为1,表示要改变一次指向,然后最短路 =========高亮!!!===== ...
- Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/config/spring/applicationContext.xml]
在搭建SpringMVC框架的时候遇到了这个问题 问题的原因: 就是没有找到applicatoincontext.xml这个文件, 因为idea自动生成的路径不正确 因此需要再web.xml里面, ( ...
- Eaton Char-Lynn Motor : Performance Of Small Displacement Motors
The small-displacement supercharged motor replaces the large-displacement motor with the speed of li ...
- 第 6 章 Cinder - 061 - Boot from Volume
Boot from Volume Volume 除了可以用作 instance 的数据盘,也可以作为启动盘(Bootable Volume). 打开 instance 的 launch 操作界面. 这 ...