Implement PHP File upload module _php tutorial

Source: Internet
Author: User
Tags php file upload
First make sure the lamp is configured successfully.

Environment: Opensuse12.2 LAMP

Today is the php file upload module to upload images and PDF documents as an example.

The steps are as follows:

Modify PHP.ini

Linux php.ini is placed under the/etc/php5/apache2 folder, the terminal runs

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

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


Use "/xxx" to find the following values in vim and modify them:

Upload_max_filesize = 10M


This option indicates the maximum byte length of the uploaded file. Default 2M, change to 10M

Post_max_size = 12M

This option represents the maximum byte length allowed for post data, the default is 8M, and the recommended setting value is slightly larger than upload_max_filesize.


Memory_limit default 128M, if the size of the file to upload more than this value, you need to modify, there is no need to modify.

Save when you are finished modifying.


Create a project

Create the Test2 folder in/srv/www/htdocs/, create two files inside: upload.html,upload_file.php. Also create a upload folder to hold the uploaded files.

First, the upload folder to handle, modify its permissions.

Terminal operation:


[Plain]
sudo chmod 777 Upload-r

sudo chmod 777 Upload-r

Here is the code listing.

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). The 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). The 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";
}
?>
Explain briefly.

The HTML file is a form that handles the interaction with the user, noting the properties of the button. That's how it works.

Select a good file, click Submit, the background processing will be given to PHP.


PHP obtains some properties of the file through the global array _file, and then handles it accordingly.

$_files["File" ["Error"] put the error code, the corresponding error is as follows:


Coding
Value
Description

Upload_err_ok
0
File Upload successfully

Upload_err_ini_size
1
File size is larger than upload_max_filesize specified value in php.ini

Upload_err_form_size
2
The size of the file is smaller than the value specified by the max_file_size of the form

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

Upload_err_no_file
4
No files are uploaded with this request

Upload_err_no_tmp_dir
6
No temp folder specified in php.ini


After uploading the file, you can see the uploaded files in the upload.

It's almost done, but it can also be extended, such as displaying a progress bar when uploading a large file, such as uploading a file to the database at the same time, and then displaying the file name on the page, click to download.

Time relationship, right here.

http://www.bkjia.com/PHPjc/477552.html www.bkjia.com true http://www.bkjia.com/PHPjc/477552.html techarticle first make sure the lamp is configured successfully. Environment: Opensuse12.2 LAMP today is the php file upload module to upload images and PDF documents as an example. The steps are 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.