So I have a working image upload php file, but its getting to the point where I need to start renaming the files being uploaded based on the date/time.
This is the code I currently have to upload the images:
Quote:
<?php
$target_dir = "uploads/";
$newFileName = $target_dir .'fileName'.'.'. pathinfo($_FILES["fileToUpload"]["name"] ,PATHINFO_EXTENSION);
$uploadOk = 1;
$imageFileType = pathinfo($_FILES["fileToUpload"]["name"] ,PATHINFO_EXTENSION);
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 (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $newFileName)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
|
What I'd like to do is add something along the lines of:
Quote:
if(isset($_POST['sub'])){
if($_FILES['userImage']['name']){
$time = date("YmdHis")."-".time();
$fileName = $_FILES['userImage']['name'];
$fileName = $time."-".$fileName ;
move_uploaded_file($_FILES['userImage']['tmp_name'], "uploads/".$fileName);
$img="uploads/".$_FILES['userImage']['name'];
}else{
echo "Something went wrong";
}
}
|
But I cant figure out where I need to edit my original code, or append the date rename part, every time I try it stops uploading the image file to the server :/
Basically, what I need to be able to do is rename image.jpg to image10112022101656.jpg (which I beleive the $time = date("YmdHis")."-".time(); should be doing, I just can't figure out where to drop that new part of code into my original, any help would be appreciated.
Also, as a secondary question on the same subject, once the file is uploaded to the directory on the server, how would I go about submitting that url to a row in my SQL database and attaching it as an image to a specific recipe?