Home > Article > Backend Development > Please take care of me if you are new to PHP. Please take care of me if you are new to PHP (8)
7. File upload
You can use PHP to implement the file upload function. Note that the client browser should be Netscape3 or above or IE3 or above. At the same time, because this program is related to your PHP configuration file (php3.ini for PHP3, php.in for PHP4) settings. Before executing this program, please check whether your PHP configuration file has the following settings:
Remove the comment character of the line ";upload_tmp_dir", that is, the semicolon ";" in front of it, so that this line will be in the php.ini document kick in. upload_tmp_dir is used to define the temporary path where uploaded files are stored. Here you can also define an absolute path for it, for example: upload_tmp_dir = d:upload Of course, your d:upload directory must have read and write permissions at this time.
If you have defined the upload path in your .php3 program, the path of the uploaded file will be based on the path defined in the .php3 program. In the following example, the receiver.php3 file specifies the directory used to store uploaded files: d:upload.
upload_max_filesize is used to limit the maximum size of uploaded files processed by PHP. It is calculated in bytes. The default value is 2097152= 2*1024*1024 bytes (2 megabytes). You can define the maximum size by modifying the default value. The size of the uploaded file.
Don’t forget to restart the Apache, IIS or PWS service after modification.
At the same time, there are several points worth noting about file uploading in PHP:
1. In the form form, set the method attribute to post and the enctype attribute to multipart/form-data;
2. In the form form, you can Add a hidden type input box with a hidden value field named MAX_FILE_SIZE. You can limit the size of the uploaded file by setting its VALUE. Of course, this value cannot exceed the upload_max_filesize in the PHP configuration file (PHP3 is php3.ini, PHP4 is php.ini). Note that this input box must be placed in front of all file type input boxes, otherwise it will be invalid;
3. After the PHP program is run, the uploaded file is placed in the temporary directory. If the uploaded file has not been renamed or moved, the file will be automatically deleted from the temporary folder at the end of the request, so we'd better upload the new uploaded file immediately to a permanent directory or change its file name.
First we need a form page for uploading files (upload.htm):