Php files Upload instance code, php files are much easier to upload than asp, and the amount of code is relatively small
Project structure:
Upload homepage:
Upload effect:
Fileupload. php
Copy codeThe code is as follows:
// 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 uploaded successfully.
* 1: The file size is exceeded. set it in the php. ini file.
* 2: The file size exceeds the value specified by the MAX_FILE_SIZE option.
* 3: only part of the file is uploaded.
* 4: no files are uploaded.
* 5: The size of the uploaded file is 0.
*/
$ Error = $ upfile ["error"]; // value returned by the system after uploading
Echo "========================
";
Echo "the name of the uploaded file is:". $ name ."
";
Echo "the Upload file type is:". $ type ."
";
Echo "the size of the uploaded file is:". $ size ."
";
Echo "the value returned by the system after the upload is:". $ error ."
";
Echo "the temporary storage path for uploaded files is:". $ tmp_name ."
";
Echo "start to move and upload files
";
// Move the uploaded temporary file to the up Directory
Move_uploaded_file ($ tmp_name, 'up/'. $ name );
$ Destination = "up/". $ name;
Echo "========================
";
Echo "upload information:
";
If ($ error = 0 ){
Echo "the file has been uploaded successfully! ";
Echo"
Image preview:
";
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 ....