Project Structure:
Upload homepage:
Upload effect:
Fileupload. php
Copy codeThe Code is as follows:
<Form action = "" enctype = "multipart/form-data" method = "post"
Name = "uploadfile"> Upload file: <input type = "file" name = "upfile"/> <br>
<Input type = "submit" value = "Upload"/> </form>
<? Php
// Print_r ($ _ FILES ["upfile"]);
If (is_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name']) {
$ Upfile = $ _ FILES ["upfile"];
// Obtain the values in the array
$ Name = $ upfile ["name"]; // file name to be uploaded
$ Type = $ upfile ["type"]; // File Upload type
$ Size = $ upfile ["size"]; // size of the uploaded file
$ Tmp_name = $ upfile ["tmp_name"]; // temporary storage path for uploaded files
// Determine whether the image is used
Switch ($ type ){
Case 'image/pjpeg ': $ okType = true;
Break;
Case 'image/jpeg ': $ okType = true;
Break;
Case 'image/gif': $ okType = true;
Break;
Case 'image/png ': $ okType = true;
Break;
}
If ($ okType ){
/**
* 0: The file is successfully uploaded. <br/>
* 1: If the file size is exceeded, set it in the php. ini file. <br/>
* 2: The file size exceeds the value specified by the MAX_FILE_SIZE option. <br/>
* 3: only part of the file is uploaded. <br/>
* 4: no files are uploaded. <br/>
* 5: the size of the uploaded file is 0.
*/
$ Error = $ upfile ["error"]; // value returned by the system after uploading
Echo "==========================< br/> ";
Echo "the name of the uploaded file is:". $ name. "<br/> ";
Echo "Upload file type:". $ type. "<br/> ";
Echo "the size of the uploaded file is:". $ size. "<br/> ";
Echo "the value returned after the upload is:". $ error. "<br/> ";
Echo "the temporary storage path for uploaded files is:". $ tmp_name. "<br/> ";
Echo "start to move uploaded files <br/> ";
// Move the uploaded temporary file to the up directory
Move_uploaded_file ($ tmp_name, 'Up/'. $ name );
$ Destination = "up/". $ name;
Echo "==========================< br/> ";
Echo "upload information: <br/> ";
If ($ error = 0 ){
Echo "the file has been uploaded successfully! ";
Echo "<br> image preview: <br> ";
Echo " ";
// Echo "alt = \" image preview: \ r file name: ". $ destination." \ r upload time: \ "> ";
} Elseif ($ error = 1 ){
Echo "exceeds the file size and is set in the php. ini file ";
} Elseif ($ error = 2 ){
Echo "exceeds the file size specified by the MAX_FILE_SIZE option ";
} Elseif ($ error = 3 ){
Echo "only part of the file is uploaded ";
} Elseif ($ error = 4 ){
Echo "no file is uploaded ";
} Else {
Echo "the size of the uploaded file is 0 ";
}
} Else {
Echo "please upload images in jpg, gif, png, and other formats! ";
}
}
?>
In the fileupload. php file:
Copy codeThe Code is as follows:
// Determine whether the image is used
Switch ($ type ){
Case 'image/pjpeg ': $ okType = true;
Break;
Case 'image/jpeg ': $ okType = true;
Break;
Case 'image/gif': $ okType = true;
Break;
Case 'image/png ': $ okType = true;
Break;
}
The above is to determine whether the file is of the image type. For more file types, refer to the tomcat/conf/web. xml file. The file types here are quite comprehensive ....