Php file Upload instance details !!!

Source: Internet
Author: User
Tags php file upload
This article mainly introduces the php file Upload instance code. if you need it, you can refer to the following form code:

The code is as follows:



Here are a few things to pay attention to. First, let's take a look at this sentence. Here we use the POST method. some browsers also support the PUT method. of course, this requires modifications to the script, which I do not recommend. 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 domain is required to limit the maximum length of the uploaded file: Here, the name must be set to MAX_FILE_SIZE, and its value is the maximum length of the uploaded file. the unit is B. here I am limited to 2 MB. Let's look at this sentence again: , Type = "file" describes the file type, so a 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:

The code is as follows:

/*************************************** **
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 ,"
";
}
// 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"

You have uploaded the file and uploaded the image to preview it:
";
Echo"
Continue Upload ";
}
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. According to 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 !!!!!!

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: info-contact@alibabacloud.com 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.