A simple PHP file Upload sample program _php tutorial

Source: Internet
Author: User
Tags php file upload
This article to give you beginners to introduce a simple PHP file Upload sample program code, there is a need to learn friends can enter the reference.
The code is as follows Copy Code

Defining prompt functions

Function alert ($msg) {

Return ';

}


Define the allowed file types

$allowType = Array (' Image/jpeg ', ' image/gif ', ' image/jpg ');


Define a path, either an absolute path, or a relative path can be

$filePath = './uploadfiledir/';

Receive form information The file value written inside it is the name value in the static page form

$file = $_files[' file '];

The first step is to determine if the uploaded file has errors

if ($file [' ERROR ']!== 0) {

Exit (Alert (' File upload error ');

}

The second step, determine the file size, here is 102400 bytes, converted to KB is 100KB

if ($file [' Size '] > 102400) {

Exit (Alert (' File too large ');

}

Step three, determine the file type

if (!in_array (Mime_content_type ($file [' tmp_name ']), $allowType)) {

Exit (Alert (' File type error '));

}

The fourth step is to determine if the path exists, and if it does not exist, create

if (!file_exists ($filePath) &&!mkdir ($filePath, 0777,true)) {

Exit (Alert (' Create directory error '));

}

Fifth step, define the name and path after uploading

$filename = Time (). ' _ '. $file [' name '];

Sixth step, copy the file

if (!copy ($file [' Tmp_name '], $filePath. $filename)) {

Exit (Alert (' Error uploading file, please retry later '));

}

Seventh step, delete temporary files

Unlink ($file [' tmp_name ']);

Prompt for upload success

echo alert (' Congratulations, upload file ['. $filename. '] Success! ');
?>

http://www.bkjia.com/PHPjc/628866.html www.bkjia.com true http://www.bkjia.com/PHPjc/628866.html techarticle This article to give you beginners to introduce a simple PHP file Upload sample program code, there is a need to learn friends can enter the reference. Code to copy code like this? PHP//Definition hints ...

  • 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.