This article describes the PHP file upload simple implementation method. Share to everyone for your reference. Specifically as follows:
File 1:index.php
Copy Code code as follows:
<form enctype= "Multipart/form-data" action= "uploadprocess.php" method= "post" name= "MyForm" >
User name: <input type= "text" name= "username"/><br/>
Password: <input type= "password" name= "pwd"/><br/>
Upload Picture: <input type= "file" Name= "Upimage" ><br/>
<input type= "Submit" Name= "sub" value= "submitted" >
</form>
File 2:uploadprocess.php
Copy Code code as follows:
<?php
$filetype _arr = Array ("Image/jpeg", "Image/png", "Image/gif", "image/gif"); type of picture allowed to upload
if (!empty ($_post[' Sub ')) {
$username = $_post[' username '];//username
$password = $_post[' pwd '];//password
if (Is_uploaded_file ($_files[' upimage '] [' tmp_name ']) {//To determine whether the image is uploaded via http
$tmp _name = $_files[' upimage ' [' tmp_name '];//upload the temporary name of the picture
Determines whether a picture type supports
if (!in_array ($_files[' upimage '] [' type '], $filetype _arr)) {
echo "Sorry, upload pictures only support";
foreach ($filetype _arr as $value) {
echo $value. ",";
}
echo "Equal type";
Exit ();
}
$exten _name = substr ($_files[' upimage '] [' Name '],strrpos ($_files[' upimage '] [' name '], ".")); /get the suffix name of the uploaded picture
$dest _name = $_server[' Document_root ']. " /php/upfile/". $username." /";//
To determine if the saved directory exists or not, create
if (!file_exists ($dest _name)) {
if (!mkdir ($dest _name)) {//If the creation is unsuccessful
echo "Create directory failed, file cannot be uploaded";
Exit ();
}
}
If a saved directory already exists
if (Move_uploaded_file ($tmp _name, $dest _name.time (). Rand (1,100). $exten _name)) {//move temporary file to saved directory and rename it
echo "Upload image success";
}else{
echo "Picture upload failed";
}
}else{
echo "Can't find the picture you uploaded, please upload it again";
}
}
?>
I hope this article will help you with your PHP program design.