PHP file upload code usage

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

This article is applicable to php beginners. It shows you how to edit the php file upload code. Before editing, you need to know a few points and understand the global variables of FILES, if you need to learn how to upload PHP files, refer to this article.

PHP file upload code compilation process

1. first determine whether to upload a file
2. If yes, check whether an error occurs during the upload.
3. If an error occurs, an error message is displayed.
4. If no error is found, determine the file type.
5. If the type meets the condition, check whether the file exists in the specified directory.
6. If not, move the file to the specified directory.

Several things that must be known when uploading files in php

$ _ FILES ['myfile'] ['name'] indicates the name of the uploaded file.
$ _ FILES ['myfile'] ['type'] indicates the type of the file to be uploaded.
$ _ FILES ['myfile'] ['SIZE'] indicates the size of the uploaded file, in bytes (B)
$ _ FILES ['myfile'] ['tmp _ name'] indicates the name of the temporary copy file in the server where the uploaded file exists, after the file is moved to the specified directory, the temporary file will be automatically destroyed.
$ _ FILES ['myfile'] ["error"] indicates the status code of an error that may occur during file upload. the status code will be described later.


Let's take a look at the HTML section.

The Code is as follows: Copy code

? <Form action = "upload. php" method = "post" enctype = "multipart/form-data">
Upload: <input type = "file" name = "myfile"/>
<Input type = "submit" name = "submit" value = "Upload"/>
</Form>

Note:

Action = "upload. php" in form indicates that when you click submit in form, the upload Command will be sent to the page called upload. php for processing. Method = "post" refers to the post method. The enctype = "multipart/form-data" attribute specifies the content type to be used when submitting this form, when a form requires binary data, such as file content, use "multipart/form-data". This attribute is required to upload files. When type = "file" in input, it is stipulated that the input should be processed as a file, and there will be a browser button behind the input.

Let's take a look at the upload. PHP processing page.

The Code is as follows: Copy code


<? Php
If ($ _ FILES ['myfile'] ['name']! = ''){
If ($ _ FILES ['myfile'] ['error']> 0 ){
Echo "error status:". $ _ FILES ['myfile'] ['error'];
} Else {
Move_uploaded_file ($ _ FILES ['myfile'] ['tmp _ name'], "uploads/". $ FILES ['myfile'] ['name']);
Echo "<script> alert (uploaded successfully !); </Script> ";
}
} Else {
Echo "<script> alert (please upload a file !); </Script> ";
}
?>

The above is super simple. Let's upgrade it now.


1. upload. php

The Code is as follows: Copy code

<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> ddd </title>
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
</Head>
<Body>
<! -- Note: 1. enctyp, 2. method = "post" -->
<Form enctype = "multipart/form-data" action = "uploadProcess. php" method = "post">
<Table>
<Tr> <td> enter the user name </td> <input type = "text" name = "username"> </td> </tr>
<Tr> <td> brief introduction file </td> <textarea rows = "7" cols = "50" name = "fileintro" style = "width: 300px; "> </textarea> </td> </tr>
<Tr> <td> upload your file </td> <input type = "file" name = "myfile"> </td> </tr>
<Tr> <td colspan = "2"> <input type = "submit" value = "Upload"> <td> </tr>
</Table>
</Form>
</Body>
</Html>

2. uploadProcess. php

The Code is as follows: Copy code


<? Php

// Receive
$ Username = $ _ POST ['username'];
$ Fileintro =$ _ POST ['fileintro'];

// Echo $ username. $ fileintro;
// Obtain the File Information
/* Echo "<pre> ";
Print_r ($ _ FILES );
Echo "</pre> ";
*/
// Obtain the file size
$ File_size = $ _ FILES ['myfile'] ['SIZE'];
If ($ file_size> 2*1024*1024 ){
Echo "<script type = 'text/javascript '> window. alert ('file cannot exceed 2 mbs') </script> ";
Exit ();
}

// Obtain the file type
$ File_type = $ _ FILES ['myfile'] ['type'];
If ($ file_type! = "Image/jpeg" & $ file_type! = "Image/pjpeg "){
Echo "the file type can only be jpg ";
Exit ();
}

// Determine whether the upload is OK
If (is_uploaded_file ($ _ FILES ['myfile'] ['tmp _ name']) {
// Obtain the uploaded file and store it in the desired directory.
$ Upload_file = $ _ FILES ['myfile'] ['tmp _ name'];

// Prevents image overwriting and creates a folder for each user
$ User_path = $ _ SERVER ['document _ root']. "/file/up/". $ username;
If (! File_exists ($ user_path )){
Mkdir ($ user_path );
}

// $ Move_to_file = $ user_path. "/". $ _ FILES ['myfile'] ['name'];
// Prevents the user from having the same upload Username
$ File_true_name = $ _ FILES ['myfile'] ['name'];
$ Move_to_file = $ user_path. "/". time (). rand (1,1000). substr ($ file_true_name, strripos ($ file_true_name ,"."));

// Echo $ upload_file. $ move_to_file;
// Transcode Chinese Characters
If (move_uploaded_file ($ upload_file, iconv ("UTF-8", "gb2312", "$ move_to_file "))){
Echo $ _ FILES ['myfile'] ['name']. "Upload successful ";
} Else {
Echo "Upload Failed ";
}
} Else {
Echo "Upload Failed ";
}

?>

Note:

Let me give you an example. For example, for an image file pic.jpg, we use strrchrfor processing, strrchr(pic.jpg,'.', and then we will return .jpg. Do you understand? This function returns the character after the position of the specified character at the end of the string. With substr (), we can get the jpg file, so that we can get the file suffix to determine whether the uploaded file meets the specified format. In this program, the specified format is placed in an array. You can add it as needed during actual use.
Next, let's look at the part of the random number file name. We can see the mt_srand () function. In this manual, we call it "play a better random number generator seed", which is actually a random number initialization function, the parameter is (double) microtime () * 1000000. If this parameter is not used, a random number is automatically set. Of course, this does not meet our needs. In this way, the random number has a certain length, ensure that the uploaded file is not named again

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.