PHP ?? ???
PHP ?? ???
PHP? ?? ??? ??? ???? ? ????.
? ?? ??? ??? ?????? ???????. ???? ???
test
|-----upload???????? # ???? ?? ???
|------form.html ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?>? ? ? ? ? ? ? ? ? ? ? ? to >
???? ???? ??? ?????? ???? ?? ?? ?????. ?? ???? ?? HTML ??? ?????.<html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">文件名:</label> <input type="file" name="file" id="file"><br> <input type="submit" name="submit" value="提交"> </form> </body> </html>? ??? form.html ??? ?????.
<關> ?? HTML ??? ?? ? ?? ?? ??? ??? ????.
· & LT form & gt; labeling ??? ?? ??? ??? ??? ? ???? ?? ?? ??? ?????. ??? ?? ???? ?? ???? ???? ??? ?? "multipart/form-data"? ?????.
·??????????????????????????????????????????????????????????????????????????????????????????????? ??? type="file" ??? ??? ??? ????? ?? ?????. ?? ?? ?????? ?? ? ? ?? ?? ?? ???? ??? ?????.
??: ???? ??? ?????? ???? ?? ??? ?? ??? ?????. ??? ? ?? ???? ?? ??? ??? ????? ??????.
??? ???? ???"upload_file.php" ???? ?? ???? ?? ??? ???? ????:
<?php if ($_FILES["file"]["error"] > 0) { echo "錯誤:" . $_FILES["file"]["error"] . "<br>"; } else { echo "上傳文件名: " . $_FILES["file"]["name"] . "<br>"; echo "文件類型: " . $_FILES["file"]["type"] . "<br>"; echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; echo "文件臨時存儲的位置: " . $_FILES["file"]["tmp_name"]; } ?>
By PHP ?? ?? ?? $_FILES? ???? ????? ????? ?? ??? ??? ???? ? ????.
? ?? ????? ??? ?? ???? ? ?? ??? "name", "type", "size", "tmp_name" ?? "error"? ? ????. ??? ?? ?????. · $ _files ["file"] ["name"] - ??? ?? ??
· $ _files [""] ["type"] - ??? ??
·??????? $_FILES["file"]["size"] ?? - ???? ??? ??(???)
·??????? $_FILES["file"]["tmp_name"] - ??? ??? ??? ?? ??? ??
·???????? $_FILES["file"]["error"] - ?? ???? ?? ???? ?? ??
???. ?? ??? ?? ??? ?????. ???? ??? ?? ???? ???? ??? ?? ??? ???? ???.
??? ??
? ??????? ?? ???? ??? ??????. ???? .gif, .jpeg, .jpg, .png ??? ???? ? ??? ?? ??? 200kB ????? ???.
<?php // 允許上傳的圖片后綴 $allowedExts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_FILES["file"]["name"]); $extension = end($temp); // 獲取文件后綴名 if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 204800) // 小于 200 kb && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "錯誤:: " . $_FILES["file"]["error"] . "<br>"; } else { echo "上傳文件名: " . $_FILES["file"]["name"] . "<br>"; echo "文件類型: " . $_FILES["file"]["type"] . "<br>"; echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; echo "文件臨時存儲的位置: " . $_FILES["file"]["tmp_name"]; } } else { echo "非法的文件格式"; } ?>
???? ?? ??
?? ???? ??? PHP ?? ??? ???? ??? ?? ???? ?????.
? ?? ???? ????? ??? ?????. ???? ??? ????? ?? ??? ???? ???:
<?php // 允許上傳的圖片后綴 $allowedExts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_FILES["file"]["name"]); echo $_FILES["file"]["size"]; $extension = end($temp); // 獲取文件后綴名 if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 204800) // 小于 200 kb && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "錯誤:: " . $_FILES["file"]["error"] . "<br>"; } else { echo "上傳文件名: " . $_FILES["file"]["name"] . "<br>"; echo "文件類型: " . $_FILES["file"]["type"] . "<br>"; echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; echo "文件臨時存儲的位置: " . $_FILES["file"]["tmp_name"] . "<br>"; // 判斷當期目錄下的 upload 目錄是否存在該文件 // 如果沒有 upload 目錄,你需要創(chuàng)建它,upload 目錄權限為 777 if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " 文件已經(jīng)存在。 "; } else { // 如果 upload 目錄不存在該文件則將文件上傳到 upload 目錄下 move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "文件存儲在: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "非法的文件格式"; } ?>
? ????? ??? ?? ????? ?????. ??? ??? "upload"?? ????? ??? ?????. .