How to Upload file using PHP with Advanced Example

In this article, I will demonstrate how to upload file in database using PHP from a client web browser to a web server using FTP (File Transfer Protocol). In order to accomplish this, we must first set up PHP such that it can accept file uploads. Launch php.ini, and then make sure the following settings are correct:

NameDescription
1. file_uploadsmust be set to (“on” or set to “true” or “1”) so that the php will accept file upload
2. upload_tm_diris where we’re going to upload file temporarily, and it uses system’s tmp dir.
3. post_max_sizemaximum size to anypost request is 8MB.
4. upload_max_filesizethe upload maximum filesize is set by default to 2MB.
5. max_execution_timehow long is the execution time basically it is set to 30 seconds
6. max_input_timeit is set to -1 meaning it has no limit
7. memory_limithow much is each script allowed to use and the default is set to 128MB.

At this point, we are going to get started on putting together our online directory. And I’m going to call it “upload,” and inside the upload folder I’m going to create a new directory that I’m going to call “uploads,” which is where all of the things that you upload will be saved.

The next step is to make a new PHP file and give it the name “upload.php.” Also make sure to keep it within the upload folder. Then, the following lines of code will be added to the upload.php file. And the secret password is:

[php]



[/php]

The code that is seen above is pretty fundamental HTML code. It will publish to itself using the upload.php file extension, and it will include certain variables in the inputs and the submit button. In addition, we are going to add some PHP code above the HTML in order to perform some processing.

The one and only difference in utilizing this HTML code is that there is an enctype attribute that reads “multipart/form-data.” This attribute is the enclosure type, and it informs us that we are not just submitting text, but that there may also be files attached.

The next step is to select “file” as the type for the input since we are now making inputs. The name of this function is “upload file.”

And another essential component of this HTML code is the code>input type=”hidden” name=”MAX FILE SIZE” value=”1000000″>/code> element. and the maximum file size, measured in bytes; this value must be declared before the file input field, and it is not allowed to be greater than what is specified in the php.ini file under the heading “upload max filesize.”

At this point, we are going to insert some PHP code directly above the HTML form. And the secret password is:

The following notifications will be displayed to us regardless of whether or not the file was successfully uploaded.

[php]
{$message}

“;}?>
[/php]

Now that we have web forms that are all set up and ready to be submitted, we need to first understand the ideas behind how to analyze the files that have been uploaded.

In order to accomplish this, we need to have a look at the newly added superglobals that are called $_FILES. Any file that was uploaded in conjunction with a POST request will be stored in this $_FILES variable.

To access this $_FILES is to say what is the name of those input field. In our case its “upload_file”. So the way use this $_FILES[‘upload_file’]. And this $_FILE[‘upload_file’] will return an associative array and will give us five pieces of data and these are the following: 

Example : $_FILES[‘upload_file’][‘name’]

  • name – it will contain the original file name
  • type – the type (“image/gif”)
  • size – the size in bytes
  • tmp_name – temp file name on the server
  • error – error code

At this time, we’re going to look at the posible error when we upload files. And below are file upload error: 

NameDescription
1.  UPLOAD_ERR_OKit means no error
2. UPLOAD_ERR_INI_SIZEit means that upload is larger than the specified upload_max_filesize
3. UPLOAD_ERR_FORM_SIZEit says the Form size was to large or Larger than MAX_FILE_SIZE
4. UPLOAD_ERR_NO_FILEit means that it is a partial upload or the file didn’t finished
5. UPLOAD_ERR_NO_FILENo file has sent all
6. UPLOAD_ERR_NO_TMP_DIRNo temporary directory
7. UPLOAD_ERR_CANT_WRITEit means that we can’t write to the disk or it is read only
8. UPLOAD_ERR_EXTENSIONfile upload stopped by extension

Now that we have this error code, let’s proceed to apply it and display it in the message. To accomplish this, add the PHP code that is shown below directly above the HTML code:

In the following piece of code, we first construct an array called upload errors. This is done so that whenever there is an error. The program will check into the array, locate the key in question, and then display the appropriate message. In addition, there is an option on the form to “submit to itself”.

Then transfer the file from the temporary directory to the “uploads” folder on our server. However, before we can relocate the file. We will need to first specify the name of the temporary directory. Then we will need to additionally specify the name of the directory in which our file will be placed.

[php]
“No errors.”,
UPLOAD_ERR_INI_SIZE => “Larger than upload_max_filesize.”,
UPLOAD_ERR_FORM_SIZE => “Larger than form MAX_FILE_SIZE.”,
UPLOAD_ERR_PARTIAL => “Partial upload.”,
UPLOAD_ERR_NO_FILE => “No file.”,
UPLOAD_ERR_NO_TMP_DIR => “No temporary directory.”,
UPLOAD_ERR_CANT_WRITE => “Can’t write to disk.”,
UPLOAD_ERR_EXTENSION => “File upload stopped by extension.”,
);
if (isset($_POST[‘submit’])){
$tmp_file = $_FILES[‘upload_file’][‘tmp_name’];
@$target_file = basename($_FILES[‘upload_file’][‘name’]);
$upload_dir = “uploads”;

if (move_uploaded_file($tmp_file,$upload_dir.”/”.$target_file)){
echo “File uploaded Succesfully”;
}else{
$error = $_FILES[‘upload_file’][‘error’];
$message = $upload_errors[$error];
}

}

?>
[/php]

And here’s all the code in the tutorial:4

[php]
“No errors.”,
UPLOAD_ERR_INI_SIZE => “Larger than upload_max_filesize.”,
UPLOAD_ERR_FORM_SIZE => “Larger than form MAX_FILE_SIZE.”,
UPLOAD_ERR_PARTIAL => “Partial upload.”,
UPLOAD_ERR_NO_FILE => “No file.”,
UPLOAD_ERR_NO_TMP_DIR => “No temporary directory.”,
UPLOAD_ERR_CANT_WRITE => “Can’t write to disk.”,
UPLOAD_ERR_EXTENSION => “File upload stopped by extension.”,
);

if (isset($_POST[‘submit’])){
$tmp_file = $_FILES[‘upload_file’][‘tmp_name’];
@$target_file = basename($_FILES[‘upload_file’][‘name’]);
$upload_dir = “uploads”;

if (move_uploaded_file($tmp_file,$upload_dir.”/”.$target_file)){
echo “File uploaded Succesfully”;
}else{
$error = $_FILES[‘upload_file’][‘error’];
$message = $upload_errors[$error];
}

}

?>

{$message}

“;}?>



[/php]

How does PHP file upload work?

Users will be able to upload files to the server by using a PHP script in conjunction with an HTML form. The files are initially moved to a temporary directory after being uploaded, and a PHP script is subsequently used to move them to their final location. The HTML form with text files, a browse button, and a submit button is revealed when the page containing the form is opened by the user.

Summary

In summary, With a simple example, this article taught you everything you need to know about file uploading in PHP. You have also seen how to make scripts that run on both the client and the server to let PHP users upload files.

Now you can try uploading multiple files with different file extensions to see what happens. Also, try to cause some errors on purpose to see how the error variable gets the error message and shows it.

If you want to learn about some other basic PHP programming ideas, you can look at our PHP Tutorial. You can learn about the basics with the help of the tutorials.

When you finish the course, you not only get full training and hands-on experience, but you also get a certification. Our Complete Web Development Career Bundle is another option.

It will teach you how to use different programming languages, integrated development environments (IDEs), and web development tools. To put it simply, the course is a good way to get better at web development. This is the end of our PHP tutorial on how to upload files.

Related Article

Leave a Comment