<body> <form action="03-post-file.php" method="post" enctype="multipart/form-data"> <!-- enctype="multipart/form-data" 必須給form表單指定該屬性,否則上傳不了文件 --> <input type="file" name="upFile"><br> <!-- input type="file" 上傳文件 --> <input type="submit" value="上傳"> </form> </body>
<?php //echo "post page"; //print_r($_FILES); // 獲取上傳文件對應的字典(對象 $fileInfo = $_FILES["upFile"]; // print_r($fileInfo); // echo "<br>"; // 獲取上傳文件的名稱 $fileName = $fileInfo["name"]; // 獲取上傳文件保存的臨時路徑 $filePath = $fileInfo["tmp_name"]; // echo $fileName; // echo "<br>"; // echo $filePath; //移動文件 move_uploaded_file($filePath, "./source/".$fileName); // 注意這里的路徑寫法, 第二個參數的字符串和變量拼接 用. 而不是JS里的 + // "./source/" 不要忘記末尾的斜杠 ?>
<body> <!-- 表單可以收集數據,把收集的數據提交到遠程服務器。 可以發送請求 GET POST --> <!-- get請求會將提交的數據拼接到URL后面 post請求會把數據放到 控制台-network-Name-Headers --> <form action="02-get.php" method="post"> <!-- action 表單提交到指定地址的服務器 method 提交的方式, get/post --> <input type="text" name="userName"><br> <input type="password" name="userPw"><br> <input type="submit" value="添加"> </form> </body>