I am using following code to upload file, every thing is working fine, i just want to upload file with new name that should be current time of uploading, Actually i want to upload file with name as upload time
<?php
$statusMsg = '';
// File upload path
$targetDir = "../uploads/";
$fileName = basename($_FILES["file"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){
// Allow certain file formats
$book_title=$_POST['book_title'];
$allowTypes = array('jpg','png','jpeg');
if(in_array($fileType, $allowTypes)){
// Upload file to server
if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
// Insert image file name into database
$insert = mysqli_query($con,"INSERT into books (book_title,book_author,book_image,book_url) VALUES ('','','".$fileName."', '.')");
if($insert){
$statusMsg = "The file ".$fileName. " has been uploaded successfully.";
}else{
$statusMsg = "File upload failed, please try again.";
}
}else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}else{
$statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.';
}
}else{
$statusMsg = 'Please select a file to upload.';
}
// Display status message
echo $statusMsg;
?>
Adding current date in to the filename received from the user
$fileName = date('Y-m-d_') . basename($_FILES["file"]["name"]);
In case they add more than one file in a day you could also add the time as well
$fileName = date('Y-m-d_His_') . basename($_FILES["file"]["name"]);
By default $_FILES['file']['name']
is the actual name of file user uploads, suppose user uploads the file 'demoImage.jpeg' from the explorer then it is the actual name assigns to $fileName
in your code, just modify this line of code as given below:
Change this
$fileName = basename($_FILES["file"]["name"]);
to
$fileName = Date('His') . $fileType;
You can change the Date function's parameter to generate specific date/time for your file name.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.