How to upload a file in PHP ?
In this article, we will learn how to upload a file using PHP. Let us first understand some basic configurations.
Approach: In your “php.ini” file, search for the “file_uploads” parameter and set it to “On” as mentioned below.
file_uploads = On
In the “index.html” file, the enctype must be multipart/form-data and the method must be POST.
<form action="fileupload.php" method="POST" enctype="multipart/form-data">
Example 1: First, we created an HTML file so that we can upload a file using a form through the post method, and also it is important to make sure that the method is post method.
In “uploading_file.php”, we first store the directory name and file information from the post method, and then we check for the existence of the directory, validity of extensions. We can check if the file size is less than the maximum file size along with the existence of the file so that we can upload the file after all the validations.
The extensions which are allowed can be changed or added in an array according to the requirements of the program.
In the “uploads” folder of the working folder, the user can find the uploaded “gfg_sample.png” file.