First, let's take a look at the code of the uploaded form:
Copy codeThe Code is as follows: <form method = "post" action = "upload. php" enctype = "multipart/form-data">
<Table border = 0 cellspacing = 0 cellpadding = 0 align = center width = "100%">
<Tr>
<Td width = 55 height = 20 align = "center"> <input type = "hidden" name = "MAX_FILE_SIZE" value = "2000000"> file: </TD>
<Td height = "16">
<Input name = "file" type = "file" value = "">
<Input type = "submit" value = "Upload" name = "B1">
</Td>
</Tr>
</Table>
</Form>
Here are a few things to pay attention to. First, let's look at this sentence <form method = "post" action = "upload. php "enctype =" multipart/form-data ">. Here we use the POST method. Some browsers also support the PUT method. Of course, you need to modify the script. I do not recommend this. Enctype = "multipart/form-data must be set in the form. In this way, the server will know that the uploaded file contains the regular form information. Remember, this is required. 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">. The name must be set to MAX_FILE_SIZE, the value is the maximum length of the uploaded file. The unit is B. Here, I am limited to 2 MB. <Input name = "file" type = "file" value = "">, type = "file" indicates the file type, this basic file upload interface is complete. Next, let's talk about how to use PHP to process uploaded files. In addition, your php. the maximum length of the uploaded file set in ini may affect your actual upload. Modify the file according to the actual situation. In addition, the PHP Upload is first uploaded to the temporary directory, which is being moved to the specified directory; you can modify the temporary directory as needed or use the default value.
Okay. The form is submitted to upload. php to see what this page has:
The PHP code is as follows: Copy codeThe Code is as follows: <? Php
/*************************************** **
Title: file upload details
Author: leehui1983 (Boss Hui)
Finish Date: 2006-12-28
**************************************** */
$ Uploaddir = "./files/"; // set the file storage directory to include/
$ Type = array ("jpg", "gif", "bmp", "jpeg", "png"); // you can specify the object type that can be uploaded.
$ Patch = "http: // 127.0.0.1/cr_downloadphp/upload/files/"; // path of the program
// Obtain the file suffix Function
Function fileext ($ filename)
{
Return substr (strrchr ($ filename, '.'), 1 );
}
// Generate a random file name Function
Function random ($ length)
{
$ Hash = 'cr -';
$ Chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxy ';
$ 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']);
// Determine the file type
If (! In_array (strtolower (fileext ($ _ FILES ['file'] ['name']), $ type ))
{
$ Text = implode (",", $ type );
Echo "You can only upload the following types of files:", $ text, "<br> ";
}
// Generate the Object Name
Else {
$ Filename = explode (".", $ _ FILES ['file'] ['name']);
Do
{
$ Filename [0] = random (10); // you can specify the length of a random number.
$ Name = implode (".", $ filename );
// $ Name1 = $ 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']) {
// Preview the output image
Echo "<center> your file has been uploaded. Upload image preview: </center> <br> <center> </center> ";
Echo "<br> <center> <a href = 'javascript: history. go (-1) '> continue upload </a> </center> ";
}
Else {
Echo "Upload Failed! ";
}
}
}
?>
You may be a little dizzy at first ~~, But it doesn't matter. After listening to me, you will find it so easy !! First, let me talk about the principle. This program uses image uploading as an example to determine whether the file type is in the image format. If so, upload the file, rename the file with a combination of random numbers and time (avoid uploading the file with the same name, so it is necessary !), Upload the file to the specified directory. After the file is successfully uploaded, the uploaded image preview is output. Here we need to explain some functions in the program. First look at return substr (strrchr ($ filename ,'. '), 1). What is the role of the strrchar () function? Let me give you an example. For example, for an image file pic.jpg, we use strrchrprocessing, strrchr(pic.jpg,'.'. Will it be returned to .jpg? This function returns the character after the position of the specified character at the end of the string. With substr (), we can get the jpg file, so that we can get the file suffix to determine whether the uploaded file meets the specified format. In this program, the specified format is placed in an array. You can add it as needed during actual use.
Next, let's look at the part of the random number file name. We can see the mt_srand () function. In this manual, we call it "play a better random number generator seed", which is actually a random number initialization function, the parameter is (double) microtime () * 1000000. If this parameter is not used, a random number is automatically set. Of course, this does not meet our needs. In this way, the random number has a certain length, ensure that the name of the uploaded file is unique. Next, we call the function to determine the file type and convert it to lower case strtolower (fileext ($ _ FILES ['file'] ['name']). here is a key Dongdong $ _ FILES, which is a super Global Array and stores the form data to be processed. If register_globals is enabled, you can directly access it, but this is not safe. See the upload interface <input name = "file" type = "file" value = "">. Based on the form name, we can get a lot of information:
$ _ FILES ['file'] ['name'] -- get the file name.
$ _ FILES ['file'] ['tmp _ name'] -- get the temporary storage location
$ _ FILES ['file'] ['SIZE'] -- get the file size.
$ _ FILES ['file'] ['type'] -- Obtain the file MIME type.
With this information, we can easily determine the file information. Is it very convenient? ^ _ ^. Next, there are some functions that need to be understood. file_exists () -- determine whether the specified directory exists. If it does not exist, we certainly cannot upload it (it seems like it is nonsense !), Move_uploaded_file -- move the uploaded file to the specified directory, and is_uploaded_file -- determines whether the file has been uploaded through http post. If the upload is successful, preview the output; otherwise, the upload fails! Success
You can use this extension, such as using JavaScript to upload multiple files, such as DZ, and AJAX to achieve no-refreshing uploads. Many blogs use this feature, finally, let's play a preview of the next two original articles.
1. I will extend this example, add the backend and database sections, implement File Upload management, review, and release in the original zone.
2. Use directory functions to manage files and release them in the new zone
Hope that interested friends will watch them at that time ~~~, Thank you !!!!!!