PHP file Upload module _ PHP Tutorial

Source: Internet
Author: User
Tags button attributes php file upload
Implements the php file Upload module. First, make sure that LAMP is configured successfully. Environment: Opensuse12.2LAMP is designed to implement the php file Upload module today. The above picture and pdf document are used as examples. Follow these steps: modify php. ini to ensure that LAMP is successfully configured.

Environment: Opensuse12.2 LAMP

What we need to implement today is the php file Upload module. the above image and pdf documents are used as examples.

The procedure is as follows:

Modify php. ini

In linux, php. ini is stored in the/etc/php5/apache2 folder, and the terminal runs

[Plain]
Sudo vim/etc/php5/apache2/php. ini

Sudo vim/etc/php5/apache2/php. ini


Use "/×××" to search for the following values in vim and modify them:

Upload_max_filesize = 10 M


This option indicates the maximum length of the uploaded file in bytes. The default value is 2 Mb, which is changed to 10 MB.

Post_max_size = 12 M

This option indicates the maximum length of bytes allowed for POST data. the default value is 8 MB. the recommended value is slightly larger than upload_max_filesize.


Memory_limit is 128 MB by default. if the size of the file to be uploaded exceeds this value, it needs to be modified.

Save the modification.


Create a project

Create two files in the/srv/www/htdocs/test2 folder: upload.html and upload_file.php. create an upload folder to store uploaded files.

First, process the upload folder and modify its permissions.

Terminal operation:


[Plain]
Sudo chmod 777 upload-R

Sudo chmod 777 upload-R

The following is the code list.

Upload.html

[Html]









Upload_file.php


[Php]

If ($ _ FILES ["file"] ["type"] = "image/png ") | ($ _ FILES ["file"] ["type"] = "application/pdf "))
{
If ($ _ FILES ["file"] ["error"]> 0)
{
Echo "Return Code:". $ _ FILES ["file"] ["error"]."
";
}
Else
{
Echo "Upload:". $ _ FILES ["file"] ["name"]."
";
Echo "Type:". $ _ FILES ["file"] ["type"]."
";
Echo "Size:". ($ _ FILES ["file"] ["size"]/1024). "Kb
";
Echo "Temp file:". $ _ FILES ["file"] ["tmp_name"]."
";

If (file_exists ("upload/". $ _ FILES ["file"] ["name"])
{
Echo $ _ FILES ["file"] ["name"]. "already exists .";
}
Else
{
Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"],
"Upload/". $ _ FILES ["file"] ["name"]);
Echo "Stored in:". "upload/". $ _ FILES ["file"] ["name"];
}
}
}
Else
{
Echo "Invalid file ";
}
?>

If ($ _ FILES ["file"] ["type"] = "image/png ") | ($ _ FILES ["file"] ["type"] = "application/pdf "))
{
If ($ _ FILES ["file"] ["error"]> 0)
{
Echo "Return Code:". $ _ FILES ["file"] ["error"]."
";
}
Else
{
Echo "Upload:". $ _ FILES ["file"] ["name"]."
";
Echo "Type:". $ _ FILES ["file"] ["type"]."
";
Echo "Size:". ($ _ FILES ["file"] ["size"]/1024). "Kb
";
Echo "Temp file:". $ _ FILES ["file"] ["tmp_name"]."
";

If (file_exists ("upload/". $ _ FILES ["file"] ["name"])
{
Echo $ _ FILES ["file"] ["name"]. "already exists .";
}
Else
{
Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"],
"Upload/". $ _ FILES ["file"] ["name"]);
Echo "Stored in:". "upload/". $ _ FILES ["file"] ["name"];
}
}
}
Else
{
Echo "Invalid file ";
}
?>
A brief explanation.

An html file is a form used to process interaction with users. pay attention to the button attributes. It runs like this.

Select a file and click "Submit". the processing in the background will be handed over to php.


Php obtains some attributes of the FILE through the global array _ FILE, and then processes them accordingly.

$ _ FILES ["file"] ["error"] contains the error code. the error code is as follows:


Encoding
Value
Description

UPLOAD_ERR_ OK
0
File uploaded

UPLOAD_ERR_INI_SIZE
1
The file size is larger than the value specified by upload_max_filesize in php. ini.

UPLOAD_ERR_FORM_SIZE
2
The file size is greater than the value specified by MAX_FILE_SIZE of the form.

UPLOAD_ERR_PARTIAL
3
Incomplete file upload (may be terminated due to a long request time)

UPLOAD_ERR_NO_FILE
4
No file is uploaded with this request

UPLOAD_ERR_NO_TMP_DIR
6
No temporary folder specified in php. ini


After the file is uploaded, you can see the uploaded file in upload.

It is almost done here, but it can be expanded. for example, a progress bar is displayed when a large file is uploaded, for example, a file is uploaded and written to the database, and the file name is displayed on the page. Click here to download the file.

The time relationship is here.

Bytes. Environment: Opensuse12.2 LAMP is designed to implement the php file Upload module today. The above picture and pdf document are used as examples. The procedure is as follows: modify php. ini...

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.