<html> <body> <form action="upload_file.php" method="post" /*action後面是信息傳遞地址,post方法不會使傳遞值在地址欄中顯示*/ enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="提交" /> </form> </body> </html>
html頁面php
<?php if ((($_FILES ["file"] ["type"] == "image/gif") || /*格式限制*/ ($_FILES ["file"] ["type"] == "image/jpeg") || ($_FILES ["file"] ["type"] == "image/png") || ($_FILES ["file"] ["type"] == "image/pjpeg")) && ($_FILES ["file"] ["size"] < 200000000)) { /*大小限制,單位是字節,還能夠寫20*1024*1024 即20MB */ if ($_FILES ["file"] ["error"] > 0) { echo "Return Code: " . $_FILES ["file"] ["error"] . "<br />"; } else { echo "Upload: " . $_FILES ["file"] ["name"] . "<br />"; echo "Type: " . $_FILES ["file"] ["type"] . "<br />"; echo "Size: " . ($_FILES ["file"] ["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; /*上傳是把文件先放緩存文件裏,而後轉存,這裏是否輸出緩存位置,這個位置是本地的相對地址.*/ if (file_exists ( "uploads/" . $_FILES ["file"] ["name"] )) { echo $_FILES ["file"] ["name"] . " already exists. "; } else { move_uploaded_file ( $_FILES ["file"] ["tmp_name"], "uploads/" . $_FILES ["file"] ["name"] );/* 轉存代碼,上傳路徑*/ echo "Stored in: " . "uploads/" . $_FILES["file"]["name"]; /*輸出轉存位置*/ } } } else { echo "Invalid file"; } ?>
上傳的PHP代碼。html