Hi you can prefix time stamp using below method
<?php
$target_dir = "uploads/";
$target_file = $target_dir . time().basename($_FILES["fileToUpload"]["name"]); // NOTICE THAT I CONCORDINATED TIME STAMP BEFORE THE FILE NAME
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
If you want to concordinate the primary key you have to insert the record first then to upload the file. If file not uploaded then you have to throw a warning to user that the image not uploaded or have to delete the record.
<?php
require 'dbConnection.php';
$stmt = "INSERT INTO TABLENAME (id, name, created) VALUES (NULL, '$name', NOW())";
$qry = mysql_query($stmt);
$lastInsertedId = mysql_insert_id();
$target_dir = "uploads/";
$target_file = $target_dir . $lastInsertedId.'_'.basename($_FILES["fileToUpload"]["name"]); // NOTICE THAT I CONCORDINATED TIME STAMP BEFORE THE FILE NAME
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
If you are using PDO connection then you have to modify slightly.