Upload image files and display images on the page
Introduction to enctype: The enctype attribute specifies the encoding type used by the browser when sending data back to the server.
Value description:
multipart/form-data: The form data is encoded as a message. Each control on the page corresponds to a part of the message, and no characters are encoded. This value is required when using a form with a file upload control. .
application/x-www-form-urlencoded: Form data is encoded as name/value pairs. This is a standard encoding format. Encode all characters before sending (default).
text/plain: The form data is encoded in plain text, which does not contain any controls or formatting characters. It is used in email processing and is rarely used elsewhere. Converts spaces to "+" symbols, but does not encode special characters. The data format is visible in the packet capture.
Create file upload.html:
<span> 1</span><span><!</span><span>DOCTYPE html</span><span>></span><span> 2</span><span><</span><span>html</span><span>></span><span> 3</span><span><</span><span>head </span><span>lang</span><span>="en"</span><span>></span><span> 4</span><span><</span><span>meta </span><span>charset</span><span>="UTF-8"</span><span>></span><span> 5</span><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><span>body</span><span>></span><span> 8</span><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><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="file"</span><span>/></span><span>10</span><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>
Create 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>$fileName</span>'>";<span>//</span><span>输出图片</span>
The final image is saved in the directory where upload.php is located and displayed on the page
The above introduces PHP study notes 12-Uploading files, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.