Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP学习笔记12-上传文件

PHP学习笔记12-上传文件

WBOY
WBOYasal
2016-08-08 09:21:47967semak imbas

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

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教程有兴趣的朋友有所帮助。

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:lvs、nginx、haproxy中转模式总结Artikel seterusnya:MySQL PDO连接与操作