PHP_GET數據獲取
阿新 • • 發佈:2018-02-20
檢驗 pre pan 分享圖片 username spa orm 判斷 upd PHP文件上傳處理01_
PHP_GET數據獲取
在
PHP
中,如果想要獲取通過get
方法提交的數據,可以通過$_GET
對象來獲取(雖然參數在地址欄
中可以查看)
-
HTML代碼: 下面就是一個簡單的表單代碼,將數據提交到
01.php
,使用get
的方式<form action="01.php" method="get" > <label for="">姓名: <input type="text" name= "userName"></label> <br/> <label for="">郵箱: <input type="text" name= "userEmail"></label> <br/> <input type="submit" name=""> </form>
-
PHP代碼:
<?php echo "<h1>GET_PAGE</h1>"; echo ‘userName:‘.$_GET[‘userName‘]; echo ‘<br/>‘; echo ‘userEmail:‘.$_GET[‘userEmail‘]; ?>
PHP_POST數據獲取
在
PHP
中,如果想要獲取通過post
方法提交的數據,可以通過$_POST
對象來獲取
-
HTML代碼: 下面就是一個簡單的表單代碼,將數據提交到
02.php
,使用post
的方式(註意:代碼中的method
改為post
<form action="02.php" method="post" > <label for="">姓名: <input type="text" name= "userName"></label> <br/> <label for="">郵箱: <input type="text" name= "userEmail"></label> <br/> <input type="submit" name=""> </form>
-
PHP代碼:
<?php echo "<h1>POST_PAGE</h1>"; echo ‘userName:‘.$_POST[‘userName‘]; echo ‘<br/>‘; echo ‘userEmail:‘.$_POST[‘userEmail‘]; ?>
POST&GET錯誤處理
當我們直接訪問
POST
&GET
頁面時由於並沒有傳遞任何數據,會因為$_GET
或$_POST
不存在對應的key而報錯.
- 處理方式1:
- 使用
array_key_exists(key, 數組)
函數來進行判斷 - 參數1: 要檢測的key
字符串
- 參數2: 檢驗的數組
- 使用
if(array_key_exists(‘name‘, $_GET)){ //如果有數據 再去讀取 }else{ // 反之 可以執行一些 其他的邏輯 }
PHP文件上傳處理01_$_FILES
對象
上傳文件時
html代碼中
需要進行如下設置:
- 在
html表單中
需要設置enctype="multipart/form-data"
- 只能
post
方式 PHP接收文件可以通過$_FILES
來獲取
-
HTML代碼:
<form action="03.fileUpdate.php" method="post" enctype="multipart/form-data"> <label for="">照片: <input type="file" name = "picture" multiple=""></label> <br/> <input type="submit" name=""> </form>
-
PHP代碼01 這部分代碼測試$_FILES文件的具體內容
<?php sleep(5);// 讓服務器休息一會 print_r($_FILES); ?>
-
現象:
- 點擊提交後,服務器沒有立即出現反應,而是休息了一會
sleep(5)
- 在
wamp/tmp
目錄下面出現了一個.tmp
文件 .tmp
文件一會就被自動刪除了- 服務器返回的內容中,有文件的名字
[name] => computer.png
,以及上傳文件保存的位置D:\wamp\tmp\php3D70.tmp
- 點擊提交後,服務器沒有立即出現反應,而是休息了一會
PHP文件上傳處理02_文件保存
剛剛演示了
$_FILES
對象的作用,以及PHP
接受上傳文件時,會先保存在一個臨時目錄下,那麽接下來我們就演示如何將臨時目錄
下面的文件保存起來
-
HTML代碼: 這部分的代碼不需要改變
<form action="03.fileUpdate.php" method="post" enctype="multipart/form-data"> <label for="">照片: <input type="file" name = "picture" multiple=""></label> <br/> <input type="submit" name=""> </form>
-
PHP代碼
move_uploaded_file()
這個函數可以處理文件 w3cSchool_move_uploaded_file函數解釋
Array ( [picture] => Array ( [name] => computer.png [type] => image/png [tmp_name] => D:\wamp\tmp\php8913.tmp [error] => 0 [size] => 5212 ) )
其中我們需要通過picture
(根據表單標簽的name
屬性決定)獲取臨時文件名
以及上傳文件名
<?php sleep(5);// 讓服務器休息一會,方便我們查看上傳的臨時文件 // 第一個參數是 規定要移動的文件 // 第二個參數是 規定文件的新位置 move_uploaded_file($_FILES[‘picture‘][‘tmp_name‘], ‘./upload/‘.$_FILES[‘picture‘][‘name‘]); ?>
PHP_GET數據獲取