PHP File Upload Simple implementation method _php skills

Source: Internet
Author: User
Tags create directory php file upload

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.