Simple PHP file Upload implementation

Source: Internet
Author: User
Tags php file upload upload php
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.

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.