How to upload a file and build a database in PHP
4 minute(s) read
|
Published on: Oct 27, 2021
Updated on: Dec 14, 2021
|
Users usually want to upload files to the server in interactive web pages. You can upload files in PHP by simple codes. You must show a form to the user, and the user must select a file and send it to your server. After this stage, the uploaded file can be saved to a directory in your server, and you can put its properties in a database.
Upload files in PHP by web users:
In interactive web pages, the user may need to send files such as image files or documents to the server. PHP language can be used for this purpose. You can upload files in PHP by simple codes.
After uploading the file, the file may be stored in a specific server's directory, and its name and address may be placed in the database. Or the original file may be stored in the database.
In the first case, whenever the file is needed, its name and address are read from the database, and then the file is read from the server.
Creating a form to load a file:
Forms allow users to select and upload files in PHP. The following code snippet shows such a form on the page:
Before creating the code needed to upload a file, check the file_uploads value in the php.ini file and ensure it has an on value.
Carefully in the code, you will notice that we have used the POST method for uploading. The encrypt attribute determines what kind of data can be uploaded. The multipart / form-data value is used to upload binary files.
In the type attribute of the input tag, we entered the value of the file. This value displays the browse button to select the file.
We have also specified in the action form attribute that after clicking the upload button, the upload.php file will be run.
Using the loaded file:
We can access the uploaded files using the $ _FILES array, which is a global variable. $ _FILES is a two-dimensional array that stores information about the uploaded file. We also need to write the desired PHP code in the upload.php file.
To use this matrix, we follow the following pattern:
Instead of index1, we put the name attribute of the input control whose type we have defined as a file, and instead of index2, we put the desired feature. Features that are saved about the uploaded file are:
name: uploaded file name
type: uploaded file type
tmp_name: path and name of the uploaded file that is now in the temp folder
error: error number. Zero means no error has occurred.
size: shows the file size in bytes
Create limitation to select when loading files:
The ability to upload files might threaten the security of your site. Therefore, it is better to consider certain rules for the files that want to be uploaded.
By writing the following code in the upload.php file, if the user selects a non-image file or larger than 20,000 bytes, he will encounter an invalid message file. Otherwise, the file will be uploaded, and its properties will be displayed.
Creating a database in PHP:
After uploading files in PHP, we need to save them in a database. There are several ways to create a database. You can use SQL code or wizard.
The first step is to log in to phpMyAdmin. Enter the word root in the username field below and leave the password blank.
You must enter the database name in the database tab and click the create button. We have created a database called test. The name of the database is displayed in the left panel. You can see that this database does not have a table by clicking on it. Therefore, the next step is to create a table in this database.
Creating a table in a database:
We can also use a wizard or SQL code to create a table. Here we use these codes to create a table:
In this table, the details of the loaded file will be saved.
Communicating with a database:
To use the created database, we need to communicate with it through PHP code. To create this connection, we use the following code:
Save the above code in a file called dbConfig.php. Then we use this file using the include command at the beginning upload.php file.
Upload the file and put its name in the database:
We create a folder called uploads on the server to put the uploaded files. We use the move_uploaded_file () function to place the uploaded file in the uploads folder.
Put the code in the upload.php file. After executing the file "file_upload.php" and selecting the appropriate file type, and clicking the upload button by the user, the uploaded image is copied to the server. Its name and other details are stored in the database.