Introduction to simple file upload function developed in PHP

Through PHP, files can be uploaded to the server.

2275.jpg

Let’s talk about the specific ideas for implementing the file upload function:

First, you need to create a file upload form<from>

Here you need to use the attributes of the <form> form and <input> tags

The enctype attribute of the <form> tag specifies which content type to use when submitting the form. Use "multipart/form-data" when your form requires binary data, such as file content.

<input> The type="file" attribute of the tag specifies that the input should be processed as a file. For example, when previewing in a browser, you'll see a browse button next to the input box.

Second, create the uploaded script .php file

By using PHP’s global array $_FILES, you Files can be uploaded from a client computer to a remote server.

$_FILES array content is as follows:

$_FILES['myFile']['name'] The original file of the client file Name       

$_FILES['myFile']['type'] The MIME type of the file, the browser needs to provide support for this information, such as "image/gif"       

$_FILES['myFile ']['size'] The size of the uploaded file, in bytes

$_FILES['myFile']['tmp_name'] The temporary file name stored on the server after the file is uploaded, usually The system defaults and can be specified in upload_tmp_dir of php.ini, but setting it with the putenv() function will not work

$_FILES['myFile']['error'] Error code related to the file upload , ['error'] was added in PHP 4.2.0 version. The following is its description: (They became constants after PHP3.0)

Third, add various Restrictions on files

For example: whether pictures exist, file size restrictions (a single file size must be less than 2MB), file format restrictions (users can only upload .gif, .jpeg, .jpg, .png files )etc.

Fourth, save the uploaded file

Save it to the server or to the database, or whether to create a local copy to save it.

I hope that through studying this course, everyone will have an understanding of how to use the file upload function.



Continuing Learning
||
<!DOCTYPE html> <html> <head> <title>简单文件上传</title> <meta charset="UTF-8"/> </head> <body> <h2>简单文件上传</h2> </body> </html>
submitReset Code