Uploading files using PHP to the server. An example of uploading files to the server (upload) in the php language Writing a script for the upload process
But in the beginning, I would like to say a few words about how files are loaded.
To do this, we need an html form with an input field like . In addition, to transfer files to the server, you need to set the form type multipart. For this, as a parameter enctype value is indicated multipart/form-data.
After we place a form with a field on the html page a browser window will display a field with the ability to select a file on the local computer.
After the user selects the desired file and clicks the "Upload" button, the form will transfer the data to the php script on the server specified in action forms. If the form action is empty, then the data will be transferred to the same file on which the form is located. All information about the uploaded file is placed in an array $_FILES. We just need to extract this information and move the file to the location we need.
Before you start writing the processing script multipart forms, you need to edit the configuration file php.ini to allow uploading files to the server.
PHP configuration file php.ini has three options related to uploading files to the server:
- file_uploads = On - allows uploading files to the server via HTTP protocol;
- upoad_tmp_dir = /tmp - sets the directory for temporary storage of uploaded files;
- upload_max_filesize = 2M - sets the maximum size of uploaded files.
So, create a new file named upload.php and copy the following code into it.