Set DateTime With FileName While Uploading in PHP

To Set DateTime With FileName While Uploading in PHP here is only 3 steps to append a timestamp to the filename in PHP,

  • Create a HTML form with input file type.
  • Create PHP code to upload the file.
  • Setting the date and time on the name of the file using PHP date() function.
Date Time With File Name While Uploading in PHP
Set DateTime With FileName While Uploading in PHP

Note:

Do not forget to add enctype="multipart/form-data" on form tag, like below.

Also Read: Display Current Date and Time in HTML using JavaScript

Create HTML Form

Also Read: How to Validate Date String in PHP

PHP Code to Append a Timestamp To The Filename

Code Explanations:

  • On the first line of code, we check the button clicked or not using PHP isset().
  • Then we check the Image file is exists or not in PHP if condition.
  • Get the current date and time in PHP using the PHP date("d-m-Y") function.
    • $time = date("d-m-Y")."-".time();
  • That time variable is appended to the file name using the concatenation operator.

$fileName = $_FILES['userImage']['name'];
$fileName = $time."-".$fileName ;

  • At the last image will upload using PHP move_uploaded_file($tempName, "path/".$fileName )

Here are the complete explanations of how we set or attach the timeWith File Name While Uploading in PHP.

Live View of Append a Timestamp To The Filename

Append a Timestamp To The Filename

To know more about handling file uploads in PHP, you can check the official documentation here, https://www.php.net/manual/en/features.file-upload.php

Also check:

Happy Coding..!

Was this article helpful?
YesNo
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.
Posts created 188

2 thoughts on “Set DateTime With FileName While Uploading in PHP

Leave a Reply

Your email address will not be published.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top