<html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file"> <br /> <input type="submit" name="submit" value="Submit"> </form> </body> </html>
<?php <span style="white-space:pre"> </span>if($_FILES["file"]["error"] > 0) { <span style="white-space:pre"> </span>echo "Upload Error: ". $_FILES["file"]["error"] . "<br />"; <span style="white-space:pre"> </span>}else { <span style="white-space:pre"> </span>echo "Upload : ". $_FILES["file"]["name"] . "<br />"; <span style="white-space:pre"> </span>echo "Type : ". $_FILES["file"]["type"] . "<br />"; <span style="white-space:pre"> </span>echo "Size : ". $_FILES["file"]["size"]/1024 . " kb<br />"; <span style="white-space:pre"> </span>echo "Store in : ". $_FILES["file"]["tmp_name"] . "<br />" ; <span style="white-space:pre"> </span>} ?>
<?php <span style="white-space:pre"> </span>if ((($_FILES["file"]["type"] == "image/gif") <span style="white-space:pre"> </span>|| ($_FILES["file"]["type"] == "image/jpeg") <span style="white-space:pre"> </span>|| ($_FILES["file"]["type"] == "image/pjpeg")) <span style="white-space:pre"> </span>&& ($_FILES["file"]["size"] < 20000)) { <span style="white-space:pre"> </span>if ($_FILES["file"]["error"] > 0) { <span style="white-space:pre"> </span>echo "Error: " . $_FILES["file"]["error"] . "<br />"; <span style="white-space:pre"> </span>}else { <span style="white-space:pre"> </span>echo "Upload: " . $_FILES["file"]["name"] . "<br />"; <span style="white-space:pre"> </span>echo "Type: " . $_FILES["file"]["type"] . "<br />"; <span style="white-space:pre"> </span>echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; <span style="white-space:pre"> </span>echo "Stored in: " . $_FILES["file"]["tmp_name"]; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}else { <span style="white-space:pre"> </span>echo "Invalid file"; <span style="white-space:pre"> </span>} ?>
<?php <span style="white-space:pre"> </span>if ((($_FILES["file"]["type"] == "image/gif") <span style="white-space:pre"> </span>|| ($_FILES["file"]["type"] == "image/jpeg") <span style="white-space:pre"> </span>|| ($_FILES["file"]["type"] == "image/pjpeg")) <span style="white-space:pre"> </span>&& ($_FILES["file"]["size"] < 20000)) { <span style="white-space:pre"> </span>if ($_FILES["file"]["error"] > 0) { <span style="white-space:pre"> </span>echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; <span style="white-space:pre"> </span>}else { <span style="white-space:pre"> </span>echo "Upload: " . $_FILES["file"]["name"] . "<br />"; <span style="white-space:pre"> </span>echo "Type: " . $_FILES["file"]["type"] . "<br />"; <span style="white-space:pre"> </span>echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; <span style="white-space:pre"> </span>echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; <span style="white-space:pre"> </span>if (file_exists("upload/" . $_FILES["file"]["name"])) { <span style="white-space:pre"> </span>echo $_FILES["file"]["name"] . " already exists. "; <span style="white-space:pre"> </span>}else { <span style="white-space:pre"> </span>move_uploaded_file($_FILES["file"]["tmp_name"], <span style="white-space:pre"> </span>"upload/" . $_FILES["file"]["name"]); <span style="white-space:pre"> </span>echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}else { <span style="white-space:pre"> </span>echo "Invalid file"; <span style="white-space:pre"> </span>} ?>
原文:http://blog.csdn.net/keshacookie/article/details/44134517