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..!

Subscribe For Latest Updates

Sign up to best of PHP Code Solutions, informed analysis and opinions on what matters to you.
Invalid email address
We promise not to spam you. You can unsubscribe at any time.

Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH, my native place in Odisha. I am a Web Programmer by profession and working on more than 50 projects to date. My passion is working on the web-based project using PHP and relate to all CMS and frameworks which is based on PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *