首页 > 后端开发 > php教程 > PHP学习笔记12-上传文件

PHP学习笔记12-上传文件

WBOY
发布: 2016-08-08 09:21:47
原创
1005 人浏览过

上传图片文件并在页面上显示出图片

enctype介绍:enctype属性指定将数据发回到服务器时浏览器使用的编码类型。

取值说明:

multipart/form-data: 窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分,不对字符编码。当使用有文件上传控件的表单时,该值是必需的。。
application/x-www-form-urlencoded: 窗体数据被编码为名称/值对。这是标准的编码格式。在发送前对所有字符进行编码(默认)。
text/plain: 窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符,邮件处理中用到,其他地方很少用。将空格转换为 "+" 符号,但不编码特殊字符。抓包可见数据形式。

创建文件upload.html:

<span> 1</span><span><span>DOCTYPE html</span><span>></span><span> 2</span><span><span>html</span><span>></span><span> 3</span><span><span>head </span><span>lang</span><span>="en"</span><span>></span><span> 4</span><span><span>meta </span><span>charset</span><span>="UTF-8"</span><span>></span><span> 5</span><span><span>title</span><span>></span>UpLoad File<span></span><span>title</span><span>></span><span> 6</span><span></span><span>head</span><span>></span><span> 7</span><span><span>body</span><span>></span><span> 8</span><span><span>form </span><span>action</span><span>="upload.php"</span><span> method</span><span>="post"</span><span> enctype</span><span>="multipart/form-data"</span><span>></span><span> 9</span><span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="file"</span><span>/></span><span>10</span><span><span>input </span><span>type</span><span>="submit"</span><span> value</span><span>="上传"</span><span>/></span><span>11</span><span></span><span>form</span><span>></span><span>12</span><span></span><span>body</span><span>></span><span>13</span><span></span><span>html</span><span>></span></span></span></span></span></span></span></span></span></span>
登录后复制

创建upload.php:

<span> 1</span> <span>php
</span><span> 2</span><span>/*</span><span>*
</span><span> 3</span><span> * Created by PhpStorm.
</span><span> 4</span><span> * User: Administrator
</span><span> 5</span><span> * Date: 2015/6/30
</span><span> 6</span><span> * Time: 19:02
</span><span> 7</span><span>*/</span><span> 8</span><span> 9</span><span>//</span><span>print_r($_FILES);//上传的所有文件都会存放在FILES数组里面</span><span>10</span><span>11</span><span>$file</span> = <span>$_FILES</span>['file'];<span>//</span><span>获取文件</span><span>12</span><span>$fileName</span> = <span>$file</span>['name'];<span>//</span><span>获取文件名</span><span>13</span><span>move_uploaded_file</span>(<span>$file</span>['tmp_name'],<span>$fileName</span>);<span>//</span><span>移动图片到当前目录,php文件所在的目录</span><span>14</span><span>15</span><span>echo</span> "<img  src="<span>%24fileName</span>" alt="PHP学习笔记12-上传文件" >";<span>//</span><span>输出图片</span>
登录后复制

最终图片被保存在upload.php所在的目录并且在页面上显示出来

以上就介绍了PHP学习笔记12-上传文件,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板