PHP File Upload Example detailed!!! _php Foundation

Source: Internet
Author: User
Tags file upload http post php file upload

First look at the upload section of the form code:

Copy Code code as follows:
<form method= "POST" action= "upload.php" enctype= "Multipart/form-data" >
<table border=0 cellspacing=0 cellpadding=0 align=center width= "100%" >
&LT;TD width=55 height=20 align= "center" ><input type= "hidden" name= "max_file_size" value= "2000000" > Files: </ Td>
&LT;TD height= ">"
<input name= "File" type= "file" value= "Browse" >
< input type= "submit" value= "Upload" name= "B1" >

Here are a few places to pay attention to, first of all look at this sentence <form method= "post" action= "upload.php" enctype= "Multipart/form-data" ", where we use the Post method, Individual browsers also support put methods, which, of course, require modifications to the script, which I do not recommend. You must set the Enctype= "Multipart/form-data" in the form so that the server knows that the upload file has regular form information, and remember that this must be set. In addition, a hidden field is required to limit the maximum length of the uploaded file: <input type= "hidden" name= "max_file_size" value= "2000000", where name must be set to Max_file_size, The value is the maximum length of the upload file, the unit is B, here I limit to 2 m. Look at this: <input name= "file" type= "file" value= "browse" >,type= "file" description of the type of files, such a basic upload file interface is completed, and then talk about how to use PHP to process uploaded files, In addition to your php.ini set the maximum length of the upload file may affect your actual upload, please modify according to the actual situation, the other PHP upload is uploaded to the temporary directory, in the move to the specified directory, the temporary directory can be modified as needed, or use the default values.
OK, form submission upload.php, to see what this page has:
The PHP code is as follows:
Copy Code code as follows:
Title: File Upload detailed
author:leehui1983 (leader of FAI)
Finish date:2006-12-28
$uploaddir = "./files/";//settings File Save directory Note contains/
$type =array ("JPG", "gif", "BMP", "JPEG", "PNG");//Set the type of file allowed to be uploaded
$patch = "";//program Path

Get file suffix name function
function Fileext ($filename)
Return substr (STRRCHR ($filename, '. '), 1);
Generate a random file name function
function Random ($length)
$hash = ' cr-';
$chars = ' abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz ';
$max = strlen ($chars)-1;
Mt_srand (Double) microtime () * 1000000);
for ($i = 0; $i < $length; $i + +)
$hash. = $chars [Mt_rand (0, $max)];
return $hash;
$a =strtolower (fileext ($_files[' file ' [' name '])];
Determining File Types
if (Strtolower (Fileext ($_files[' file ' [' name ']), $type))!in_array
$text =implode (",", $type);
echo "You can only upload the following types of files:", $text, "<br>";
Generate file name for target file
$filename =explode (".", $_files[' file '] [' name ']);
$filename [0]=random (10); Set the length of a random number
$name =implode (".", $filename);
$name 1= $name. " MCNCC ";
$uploadfile = $uploaddir. $name;
while (File_exists ($uploadfile));
if (move_uploaded_file ($_files[' file '] [' tmp_name '], $uploadfile)) {

if (is_uploaded_file ($_files[' file '] [' tmp_name '])) {
Output Picture Preview
echo <center> your files have been uploaded upload picture preview: </center><br><center></ Center> ";
echo "<br><center><a href= ' Javascript:history.go ( -1) ' > Continue to upload </a></center>";
echo "Upload failed! ";

Just look at these you may be a little dizzy ~ ~, but it doesn't matter, listen to me finish, you will find this thing so easy!! First I talk about the principle, the program to pass pictures for example, first to determine whether the file type is picture format, if the upload file, random number and time of the combination of renaming the file (to avoid uploading files duplicate, this is necessary!)! ), then upload the file to the specified directory, the successful upload will output the upload of the picture preview. Here are some explanations for some of the functions in the program. First Look at return substr ($filename, '. '), 1, the Strrchar () function has any effect, I give an example you know, such as a picture file pic.jpg, we use STRRCHR processing, STRRCHR ( Pic.jpg, '. '), it will return. jpg, do you understand? This function returns the character of the specified character after the last occurrence of the string. With substr () we can take JPG so we get the suffix name of the file to determine if the uploaded file matches the specified format. This program places the specified format in an array, which can be added as needed when it is actually used.
Next look at the random number of file name parts, we see Mt_srand () This function, the manual called him "sow a better random number generator seed", in fact, is the initialization of a random number of functions, parameters are (double) microtime () * 1000000, here if not this is the parameter will automatically set a random number, of course, this does not meet our needs, so, the random number has a certain length, to ensure that the upload file does not duplicate. Next, we call the function that determines the file type and converts it to lowercase strtolower ($_files[' file ' [' name ']), and here's a very critical fileext, a super global array that holds the form data that needs to be processed , if Register_globals is turned on, it can also be accessed directly, but this is not safe. Look at that upload interface <input name= "file" type= "file" value= "Browse", according to this form name, we can get a lot of information:
$_files[' file ' [' name ']--get filename
$_files[' file ' [' Tmp_name ']--get temporary storage location
$_files[' file ' [' Size ']--the size of the document
$_files[' file ' [' type ']--gets the MIME type
With this information, we can easily judge the file information, is it convenient? ^_^, then there are a few functions to understand, file_exists ()--to determine whether the specified directory exists, does not exist we certainly can not upload (like nonsense!) , move_uploaded_file--moves the upload file to the specified directory, is_uploaded_file--to determine if the file has been uploaded via an HTTP post. Successful upload, we will output preview, otherwise output upload failed! Done
We can expand according to this, such as with JS to achieve multiple file upload, such as DZ upload effect, and then a little deeper with Ajax to achieve no refresh upload, many blogs have adopted, and finally play under the next two original articles of the notice
1 I will extend this example to increase the background and the database section, implementation upload file management, audit, will be published in the original area.
2 using Directory functions to achieve file management, will be published in the novice area
Hope interested friends to watch ~ ~ ~, THANK!!!!!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.