First, make sure that lamp is configured successfully.
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
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:
sudo chmod 777 upload -R
The following is the code list.
Upload.html
Upload_file.php
<?phpif(($_FILES["file"]["type"] == "image/png")||($_FILES["file"]["type"] == "application/pdf")) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; 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.