I am creating a social site that allows people to upload pictures. I am using tempnam()
to make each image name unique. But I have a problem.
The tempnam()
function seems to be saving the full path in the DB even though I am not trying to use the full path and because of that I can't retrieve the pictures from the folder. There doesn't seem to be any other questions related to mine. Can someone help me ?
$picToUpload = tempnam("assets/images/profile_pics/", '');
$withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $picToUpload);
$fileExtension = ".png";
rename($picToUpload, $withoutExt . $fileExtension);
$new_file_name = $withoutExt . $fileExtension;
if( !move_uploaded_file($file_tmp, $picToUpload)) {
$errors = "Error uploading files";
die();
}
$path = move_uploaded_file($new_file_name, "assets/images/profile_pics/");
$file_path = $new_file_name;
unlink($picToUpload);
$stmt = $con->prepare("UPDATE users SET profile_pic = ? WHERE username = ?");
$stmt->bind_param('ss', $file_path, $username);
$stmt->execute();
$stmt->close();
header('Location: profile.php');
exit();