For the safety of the website, certainly does not let upload php file, if someone enters your backstage, uploads a php file, your website source code, all saves becomes his, the direct packing looks your code. So be sure to control the upload of the directory and file types, generally can only upload pictures.
Create a File Upload form
It is useful to allow users to upload files from a form.
Take a look at this HTML form for uploading files:
Copy Code code as follows:
<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>
Please note the following information about this form:
The Enctype property of the <form> label stipulates what type of content to use when submitting the form. Use "Multipart/form-data" when the form requires binary data, such as the contents of the file.
The type= "file" attribute of the <input> label stipulates that the input should be processed as a file. For example, when you preview in a browser, you see a navigation button next to the input box.
Note: Allowing users to upload files is a huge security risk. Only allow trusted users to perform file upload operations.
Create an upload script
The "upload_file.php" file contains code for uploading files:
Copy Code code as follows:
<?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 PHP's global array $_files, you can upload files from a client computer to a remote server.
The first parameter is the input name of the form, and the second subscript can be "name", "type", "Size", "tmp_name", or "error". Just like this:
Copy Code code as follows:
$_files["File" ["Name"]-the name of the file being uploaded
$_files["File" ["Type"]-type of file being uploaded
$_files["File" ["Size"]-the size of the file being uploaded, in bytes
$_files["File" ["Tmp_name"]-name of the temporary copy of the file stored on the server
$_files["File" ["Error"]-error code caused by file upload
This is a very simple way to upload files. For security reasons, you should increase the restrictions on what users have permission to upload files.
Upload Restrictions
In this script, we have added restrictions on file uploads. Users can only upload. gif or. jpeg files, the file size must be less than KB:
Copy Code code as follows:
<?php
if (($_files["File" ["type"] = = "Image/gif")
|| ($_files["File"] ["type"] = = "Image/jpeg")
|| ($_files["File"] ["type"] = = "Image/pjpeg")
&& ($_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";
}
?>
Note: For IE, the type of recognition JPG file must be pjpeg, for FireFox, must be JPEG.
Save uploaded files
The above example creates a temporary copy of the uploaded file in the server's PHP Temp folder.
This temporary copy file disappears at the end of the script. To save the uploaded file, we need to copy it to another location:
Copy Code code as follows:
<?php
if (($_files["File" ["type"] = = "Image/gif")
|| ($_files["File"] ["type"] = = "Image/jpeg")
|| ($_files["File"] ["type"] = = "Image/pjpeg")
&& ($_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 above script detects if the file already exists, and if it does not, copies the file to the specified folder.
Note: This example saves the file to a new folder named "Upload".