Problem:
① How to control the file types uploaded by users
② How to control the size of files uploaded by users
③ How to prevent users from overwriting Images
④ How to prevent conflicting file names uploaded by the same user
Answer:
① You can use the PHP Global Array $ _ FILES ['myfile'] ['type'] to determine the type of the uploaded file.
② You can use the PHP Global Array $ _ FILES ['myfile'] ['SIZE'] to determine the size of the uploaded file.
③ You can dynamically create folders based on the user name you enter.
③ Using time (), Rand (), the generated number to name the file can solve the file name conflict problem.
PHP codes:
1 <? PHP
2 Echo "<PRE> ";
3 Print_r ( $ _ Files );
4 Echo "<PRE> ";
5 $ File_size = $ _ Files ['Myfile'] ['SIZE']; // Get the file size
6 Echo "Current file size :". $ File_size . "B =". (INT) $ File_size /(1024). "kb <br> ";
7 $ File_type = $ _ Files ['Myfile'] ['type'];
8 // How to control the file types uploaded by users
9 If ( $ File_type = 'Image/JPEG '| $ File_type = 'Image/pjpeg '){ // Only JPG images can be uploaded.
10 // How to control the size of uploaded files
11 If ( $ File_size > 2*1024*1024 ){
12 Echo "The file size is limited to 2 MB ...";
13 // Echo "<meta content = \" 3, http://www.baidu.com \ "http-equiv = \" Refresh \ "/>"; // page Jump
14 Exit ();
15 }
16 If ( Is_uploaded_file ( $ _ Files ['Myfile'] ['tmp _ name']) { // Use the if statement to determine whether the object has been uploaded successfully
17 $ Move_from = $ _ Files ['Myfile'] ['tmp _ name'];
18 // How to Prevent Users From overwriting images ==> you can create folders based on the user name you enter
19 $ User_path = $ _ Server ['Document _ root']. "/upload /". $ _ Post ['Name']; // Move the uploaded file to the desired directory.
20 If (! File_exists ( $ User_path )){ // If this folder does not exist, create it. Next, move the image to this folder.
21 Mkdir ( $ User_path );
22 }
23 // How to prevent conflict of file names uploaded by the same user
24 $ Suffix = Substr ( $ _ Files ['Myfile'] ['name'], Strrpos ( $ _ Files ['Myfile'] ['name'], '.'); // Get File suffix
25 $ Move_to = $ User_path ."/". Time (). Rand (). $ Suffix ; // Time (), Rand () can solve the file name conflict problem.
26 If ( Move_uploaded_file ( $ Move_from , Iconv ("UTF-8", "GBK ", $ Move_to ))){
27 Echo "File Uploaded successfully! ";
28 Echo "Path :".$ Move_to ;
29 // Echo "<meta content = \" 3, http://www.baidu.com \ "http-equiv = \" Refresh \ "/> ";
30 }
31 } Else {
32 Echo "File Upload Failed ...";
33 // Echo "<meta content = \" 3, http://www.baidu.com \ "http-equiv = \" Refresh \ "/>"; // page Jump
34 Die ();
35 }
36 }
37 Else {
38 Echo "<SCRIPT> alert ('only images can be uploaded, and only images in JPG format are supported '); </SCRIPT> ";
39 // Echo "<meta content = \" 3, http://www.baidu.com \ "http-equiv = \" Refresh \ "/>"; // page Jump
40 Die ();
41 }
42 ?>
Form:
< Form Action = "Upload. php" Method = "Post" Enctype = "Multipart/form-Data" >
User name: < Input Name = "Name" Type = "Text" >
File description: < Textarea Name = "File_info" > </ Textarea >
< Input Type = "File" Name = "Myfile" />
< Input Type = "Submit" Value = "Upload" />
</ Form >
Result:
File name before upload: 1.jpg
File name before upload:13222172242955. jpg
RelatedArticle: File Upload Functions
- Address: http://www.cnblogs.com/hongfei/articles/uploadfiles.html
- Reprinted please note