I am using W3Schools example of file upload using PHP, I know they aren't the best when it comes to coding as it can be outdated and bad etc., but I don't need anything too fancy. But I am wondering how to change the file name when it is uploaded.
W3Schools code (slightly edited):
<?php
$username = $_POST['username'];
$target_dir = '../forum/uploads/'.strtolower($username).'/';
$target_file = $target_dir.'profile'.basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(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) {
$uploadOk = 1;
} else {
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
unlink($target_file);
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 1) {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "Profile picture updated";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
I want to change the name to just profile, right now when I upload using this code it will upload the file as profile followed by the name of the file, like so, profilexxxx.jpeg.