adding a time stamp to the file name using php
adding a time stamp to the file name using php
I need to rename files uploaded to server using php and add a timestamp on the file. I have written a code and its showing this eg: logicgates.docx-29-Aug-2018 19-55-36.docx
instead of : logicgates-29-Aug-2018 19-55-36.docx without the file extension in the middle of the filename.
logicgates.docx-29-Aug-2018 19-55-36.docx
logicgates-29-Aug-2018 19-55-36.docx
<?php
session_start();
date_default_timezone_set('Africa/Harare');
$date = date("d-M-Y H-i-s");
//$time = time("h-i-sa");
$targetfolder = "uploads/";
$allowedMimes = ['application/pdf','application/msword','text/plain','application/vnd.openxmlformats-officedocument.wordprocessingml.document',];
$targetfolder = $targetfolder . basename( $_FILES['file']['name']) ;
$type=pathinfo($targetfolder,PATHINFO_EXTENSION);
$ok=1;
$file_type=$_FILES['file']['type'];
if (in_array($_FILES['file']['type'], $allowedMimes))
if(move_uploaded_file($_FILES['file']['tmp_name'], $targetfolder))
//add date and time stamp to the uploaded file
if (file_exists($file_type))
$_SESSION['message']= "Sorry, file already exists. Please rename if you still want to upload it.";
header("location:lecsubmit?error") ;
else
rename("uploads/".$_FILES['file']['name'],"uploads/".$_FILES['file']['name']."-".$date.".".$type);
$_SESSION['message'] ="The file ". basename( $_FILES["file"]["name"]). " submitted successfully.";
header("location:lecsubmit?done") ;
else
$_SESSION['message']= "Sorry, your file was not uploaded.";
header("location:lecsubmit?error") ;
else
$_SESSION['message']= "You may only upload PDFs, DOCXs, DOCs or TXT files..";
header("location:lecsubmit?error") ;
?>
time()
Clearly
$_FILES['file']['name'] contains the full file name with the file extension. Instead of using $_FILES['file']['name'], take its value, remove the extension, and add the date to the end, then put the extension back.– John Conde
Aug 29 at 18:12
$_FILES['file']['name']
$_FILES['file']['name']
at @JohnConde it would have been better if you had shown the code.
– Rayobeats
Aug 29 at 20:07
2 Answers
2
Find the last dot ".", cut the left part, add timestamp and add the right part:
$file = "uploads/" . $_FILES['file']['name'];
$lastDot = strrpos($file, '.');
$newFile = substr($file, 0, $lastDot) . "-$date." . substr($file, $lastDot + 1);
rename($file, $newFile);
EDIT:
Changed str_replace by strrpos aproach suggested by @Elementary.
This is a really bad way to proceed eg doc.123manuel.doc will be 123manuel.is it what the op want ?
– Elementary
Aug 29 at 18:47
nope, look better, I´m replacing ".doc", not just "doc".
– Charles Cavalcante
Aug 29 at 18:51
but if the name of the file is "my.document.doc" the result will be "myument.timestamp.doc", this will be wrong
– Charles Cavalcante
Aug 29 at 19:06
So you can upvote my answer and edit yours in order to fit requirement. You can use str_replace but you need to specify the last occurrence of dot as offset to start replacement.
– Elementary
Aug 29 at 19:38
This worked perfect @CharlesCavalcante.
– Rayobeats
Aug 29 at 20:05
you can remove the extension first:
rename("uploads/".$_FILES['file']['name'],"uploads/".substr($_FILES['file']['name'],0,strrpos($_FILES['file']['name'],'.'))."-".$date.".".$type);
this is wrong, if the file name contains any dots, like "my.file.doc" the result will be "my.timestamp.doc" instead of "my.file.timestamp.doc"
– Charles Cavalcante
Aug 29 at 18:33
I look only for the last occurrence of the dot. You must learn more about strrpos. Yours will replace any occurence of type in the filename and is wrong. Eg doc.doxit.doc will be transform to .doxit
– Elementary
Aug 29 at 18:45
I don´t see the additional "r" on you strrpos, sorry, you are right
– Charles Cavalcante
Aug 29 at 19:04
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Use only
time()function.– akshaypjoshi
Aug 29 at 18:11