PHP File Upload code writing process 1. First determine whether to upload the file 2. If there is any more to determine whether the upload error 3. If an error occurs, an error message 4 is indicated. If no error is found, then determine the file type 5. If the type meets the criteria, then determine whether the file 6 is present in the specified directory. If you do not move the file to the specified directory, you must know a few things to upload the file in PHP.
Copy CodeDescription: The form of the action= "upload.php" refers to the click on the form of the submission, the upload command will be sent to this page called upload.php to deal with. Method= "POST" means to send by post, enctype= "Multipart/form-data" attribute specifies what type of content to use when submitting this form, and when the form requires binary data, such as the file content, use the "multipart/ Form-data ", this property is necessary if you want to upload a file. The type= "file" in input specifies that the input should be treated as a file, and there will be a browse button behind input. A PHP processing page upload.php
- if ($_files[' myfile ' [' name ']! = ') {
- if ($_files[' myfile ' [' Error '] > 0) {
- echo "Error status:". $_files[' myfile ' [' Error '];
- } else {
- Move_uploaded_file ($_files[' myfile ' [' Tmp_name '], "uploads/". $files [' myfile '] [' name ']);
- echo "";
- }
- } else{
- echo "";
- }
- ?>
Copy CodeUpgrade the above PHP code. 1, upload.php
- PHP File Upload Code _bbs.it-home.org
-
-
Copy Code2, uploadprocess.php
Receive
- $username =$_post[' username '];
- $fileintro =$_post[' Fileintro '];
echo $username. $fileintro;
- Get file information
- /* echo "
"; - Print_r ($_files);
- echo "
";
- */
- Get the size of a file
- $file _size=$_files[' myfile ' [' Size '];
- if ($file _size>2*1024*1024) {
- echo "";
- Exit ();
- }
- Get file type
- $file _type=$_files[' myfile ' [' type '];
- if ($file _type!= "Image/jpeg" && $file _type!= "Image/pjpeg") {
- echo "File type can only be in JPG format";
- Exit ();
- }
- Determine if the upload is OK
- if (Is_uploaded_file ($_files[' myfile ' [' tmp_name '])) {
- Get uploaded files to the directory you want
- $upload _file=$_files[' myfile ' [' tmp_name '];
Prevent picture overlay problems, create a folder for each user
- $user _path=$_server[' Document_root ']. " /file/up/". $username;
- if (!file_exists ($user _path)) {
- mkdir ($user _path);
- }
- $move _to_file= $user _path. " /". $_files[' myfile ' [' name '];
- Prevent users from uploading the same user name issue
- $file _true_name=$_files[' myfile ' [' name '];
- $move _to_file= $user _path. " /". Time (). Rand (1,1000). substr ($file _true_name,strripos ($file _true_name,". "));
- Echo $upload _file. $move _to_file;
- Chinese to Transcode
- if (Move_uploaded_file ($upload _file,iconv ("Utf-8", "gb2312", "$move _to_file"))) {
- echo $_files[' myfile ' [' Name ']. " Upload Success ";
- }else{
- echo "Upload failed";
- }
- }else{
- echo "Upload failed";
- }
- ?>
Copy CodeNote: For example a picture file pic.jpg, with STRRCHR processing, STRRCHR (pic.jpg, '. '), it will return. jpg, do you understand? The function returns the character of the specified character after the position of the last occurrence of the string. With substr () we can take the JPG, so that we get the file suffix, to determine whether the upload file conforms to the specified format. This program puts the specified format in an array, which can be added as needed when actually used. Look at the generation of random number file name section, see Mt_srand () This function, the manual called him "sow a better random number generator seed", is actually the initialization of a random number function, the parameter is (double) microtime () * 1000000, here if not this is the parameter will automatically set a random number, of course, this does not meet the need, so that the random number has a certain length, to ensure that the upload file does not have duplicate name. |