上傳圖片檔案並在頁面上顯示出圖片
enctype介紹:enctype屬性指定將資料傳送回伺服器時瀏覽器使用的編碼類型。
取值說明:
multipart/form-data: 窗體資料被編碼為一條訊息,頁上的每個控制項對應訊息中的一個部分,不對字元編碼。當使用有文件上傳控制項的表單時,該值是必要的。 。
application/x-www-form-urlencoded: 窗體資料被編碼為名稱/值對。這是標準的編碼格式。在發送前對所有字元進行編碼(預設)。
text/plain: 窗體資料以純文字形式進行編碼,其中不含任何控製或格式字符,郵件處理中用到,其他地方很少用。將空格轉換為 "+" 符號,但不編碼特殊字元。抓包可見資料形式。
創建文件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>
創建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>
最終圖片被保存在upload.php所在的目錄並且在頁面上顯示出來
以上就介紹了PHP學習筆記12-上傳文件,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。