With PHP, it is possible to upload files to the server.
ÎÒÃÇ¿ÉÒÔͨ¹ýPHP°ÑÎļþÉÏ´«µ½·þÎñÆ÷¡£
Create an Upload-File Form
´´½¨Ò»¸öÎļþÉÏ´«µÄ±íµ¥
To allow users to upload files from a form can be very useful.
¸øÓû§Ìṩһ¸ö×ÔÐÐÉÏ´«ÎļþµÄ±íµ¥ÊǺÜÓбØÒªµÄ¡£
Look at the following HTML form for uploading files:
¿´Ò»ÏÂʹÓÃHTMLÊéдµÄÎļþÉÏ´«±íµ¥µÄ¾ßÌåд·¨£º
<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>
|
Notice the following about the HTML form above:
ÉÏÊöHTML±íµ¥µÄ×¢Òâµã£º
- The enctype attribute of the <form> tag specifies which content-type to use when submitting the form. "multipart/form-data" is used when a form requires binary data, like the contents of a file, to be uploaded
<form>±êÇ©ÖеÄenctypeÊôÐÔÖ¸¶¨Á˵±Ìá½»±íµ¥Ê±£¬¸ÃʹÓÃÔõÑùµÄÄÚÈÝÀàÐÍ[content-type]£»µ±±íµ¥ÒªÇóʹÓöþ½øÖÆÊý¾Ýʱ£¬ÎÒÃÇʹÓÃ"multipart/form-data"£¬È磺ÉÏ´«ÎļþµÄÄÚÈÝ¡£
- The type="file" attribute of the <input> tag specifies that the input should be possessed as a file. For example, when viewed in a browser, there will be a browse-button next to the input field
<input>±êÇ©µÄtype="file"ÊôÐÔÖ¸¶¨ÁËÊäÈëÐÅÏ¢[input]±ØÐë°üº¬ÔÚÎļþÄÚ¡£¾Ù¸öÀý×ÓÀ´Ëµ£¬µ±ÎÒÃÇä¯ÀÀÍøÒ³Ê±£¬ÔÚÊäÈë¿òµÄÅԱ߻áÓÐÒ»¸öbrowse°´Å¥¡£
Note: Allowing users to upload files is a big security risk. Only permit trusted users to perform file uploads.
×¢Ò⣺ÔÊÐíÓû§ÉÏ´«ÎļþÕâÑùµÄ×ö·¨´æÔÚמ޴óµÄ°²È«Òþ»¼¡£ËùÒÔÎÒÃÇÓ¦¸ÃÖ»ÔÊÐíÎÒÃÇÐÅÈεÄÓû§ÉÏ´«ËûÃǵÄÎļþ¡£
Create The Upload Script
½¨Á¢Ò»¸öÉÏ´«½Å±¾³ÌÐò[Upload Script]
The "upload_file.php" file contains the code for uploading a file:
"upload_file.php"Îļþ°üº¬ÁËʵÏÖÎļþÉÏ´«¹¦ÄܵĴúÂ룬¾ßÌåÈçÏ£º
<?php if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"]; } ?>
|
By using the global PHP $_FILES array you can upload files from a client computer to the remote server.
ͨ¹ýʹÓÃͨÓõÄPHP $_FILESÊý×é[global PHP $_FILES array]£¬Äã°ÑÒÔ°ÑÄãÔÚ±¾»úÉϵÄÎļþÉÏ´«µ½Ô¶³Ì·þÎñÆ÷ÉÏ¡£
The first parameter is the form's input name and the second index can be either "name", "type", "size", "tmp_name" or "error". Like this:
µÚÒ»¸ö²ÎÊýÊÇ±íµ¥µÄÊäÈëÃû³Æ£¬µÚ¶þ¸öË÷ÒýÏî[index]¿ÉÒÔ°üº¬"name", "type", "size", "tmp_name" »ò "error"£¬¾ßÌåÈçÏ£º
- $_FILES["file"]["name"] - the name of the uploaded file
$_FILES["file"]["name"]£ºÐèÒªÉÏ´«µÄÎļþµÄÃû³Æ
- $_FILES["file"]["type"] - the type of the uploaded file
$_FILES["file"]["type"]£ºÐèÒªÉÏ´«µÄÎļþµÄÀàÐÍ
- $_FILES["file"]["size"] - the size in bytes of the uploaded file
$_FILES["file"]["size"]£ºÐèÒªÉÏ´«µÄÎļþµÄ×Ö½ÚÊý´óС
- $_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server
$_FILES["file"]["tmp_name"]£º´æ´¢ÓÚ·þÎñÆ÷ÖеÄÎļþ¸±±¾µÄÃû³Æ
- $_FILES["file"]["error"] - the error code resulting from the file upload
$_FILES["file"]["error"]£ºÎļþÉÏ´«Ê±³öÏֵĴíÎó´úÂë
This is a very simple way of uploading files. For security reasons, you should add restrictions on what the user is allowed to upload.
ÉÏ´«ÎļþµÄ·½·¨ÆäʵºÜ¼òµ¥¡£³öÓÚ¶Ô°²È«ÒòËØµÄ¿¼ÂÇ£¬Äã±ØÐë¶ÔÓµÓÐÎļþÉÏ´«È¨ÀûµÄÓû§×÷³öÑϸñµÄÏÞÖÆ¡£
Restrictions on Upload
ÉÏ´«ÏÞÖÆ
In this script we add some restrictions to the file upload. The user may only upload .gif or .jpeg files and the file size must be under 20 kb:
ÔÚÏÂÃæµÄ½Å±¾ÖУ¬ÎÒÃǶÔÎļþµÄÉÏ´«×÷ÁËһЩÏÞÖÆ´ëÊ©¡£Óû§Ö»ÄÜÉÏ´«À©Õ¹ÃûΪ“.gif”»òÀ©Õ¹ÃûΪ“.jpeg”µÄÎļþ£¬²¢ÇÒÎļþ±ØÐëСÓÚ20kb£º
<?php
if (($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg")
&& ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"]; } } else { echo "Invalid file"; }
?>
|
Saving the Uploaded File
±£´æÒÑÉÏ´«µÄÎļþ
The examples above create a temporary copy of the uploaded files in the PHP temp folder on the server.
ÔÚÉÏÊöÀý×ÓÖУ¬ÎÒÃÇÔÚ·þÎñÆ÷¶ËµÄPHPÁÙʱÎļþ¼ÐÖд´½¨ÁËÒ»¸öÒÑÉÏ´«ÎļþµÄÁÙʱ¸±±¾¡£
The temporary copied files disappears when the script ends. To store the uploaded file we need to copy it to a different location:
µ±½Å±¾½âÊͺó£¬Õâ¸öÁÙʱµÄ¸±±¾Îļþ¾Í»á×Ô¶¯Ïûʧ¡£ÎªÁË´æ´¢ÒÑÉÏ´«µÄÎļþ£¬ÎÒÃÇÐèÒª°ÑËü¸´ÖƵ½²»Í¬µÄµØ·½¡£
<?php if (($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg")
&& ($_FILES["file"]["size"] < 20000)) { 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("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?>
|
The script above checks if the file already exists, if it does not, it copies the file to the specified folder.
ÉÏÊö½Å±¾¼ì²éÁËÖ¸¶¨µÄÎļþÊÇ·ñÒѾ´æÔÚ£»Èç¹û²»´æÔÚ£¬Ëû½«°ÑÎļþ¸´ÖƵ½Õâ¸öÖ¸¶¨µÄÎļþ¼ÐÄÚ¡£