1. 程式人生 > >PHP_GET數據獲取

PHP_GET數據獲取

檢驗 pre pan 分享圖片 username spa orm 判斷 upd

  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代碼中需要進行如下設置:

  1. html表單中需要設置enctype="multipart/form-data"
  2. 只能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數據獲取