This article mainly introduces the php file Upload simple implementation method, analyzes the file upload process in the form of a complete instance, has some reference value, for more information about how to upload php files, see the following example. Share it with you for your reference. The details are as follows:
File 1: index. php
The code is as follows:
File 2: uploadProcess. php
The code is as follows:
<? Php
$ Filetype_arr = array ("image/jpeg", "image/png", "image/gif", "image/gif"); // specifies the image type that can be uploaded.
If (! Empty ($ _ POST ['sub']) {
$ Username = $ _ POST ['username']; // User name
$ Password = $ _ POST ['pwd']; // password
If (is_uploaded_file ($ _ FILES ['upimag'] ['tmp _ name']) {// You can check whether an image is uploaded over http.
$ Tmp_name = $ _ FILES ['upimage'] ['tmp _ name']; // temporary name of the uploaded image
// Determine whether the image type is supported
If (! In_array ($ _ FILES ['upimage'] ['type'], $ filetype_arr )){
Echo "Sorry, only images can be uploaded ";
Foreach ($ filetype_arr as $ value ){
Echo $ value .",";
}
Echo "and other types ";
Exit ();
}
$ Exten_name = substr ($ _ FILES ['upimag'] ['name'], strrpos ($ _ FILES ['upimag'] ['name'], ". "); // Get the suffix of the uploaded image
$ Dest_name = $ _ SERVER ['document _ root']. "/php/upfile/". $ username ."/";//
// Determine whether the saved directory exists. if it does not exist, create
If (! File_exists ($ dest_name )){
If (! Mkdir ($ dest_name) {// If creation fails
Echo "failed to create directory, file cannot be uploaded ";
Exit ();
}
}
// If a directory already exists
If (move_uploaded_file ($ tmp_name, $ dest_name.time (). rand (1,100). $ exten_name) {// Move the temporary file to the saved directory and rename it
Echo "image uploaded ";
} Else {
Echo "image Upload failed ";
}
} Else {
Echo "the image you uploaded cannot be found. please upload it again ";
}
}
?>
I hope this article will help you with php programming.