I have the following code that is used to load user images from a database when their information is displayed.
I was attempting to write it in a way in which it would check if the user already has an image tied to their user id in the database and if so, would leave the image alone and if not, would display a “missing.jpg”/default user image.
I’ve tried the following, but right now it seems the code is overwriting existing images and replacing them with the missing.jpg image and I don’t know why.
I’d appreciate somebody taking a look and showing me why that is.
//Images
$thisScript = $_SERVER["SCRIPT_FILENAME"];
$dirName = dirname($thisScript);
$relative_path = "images/headshots/".$this->id.".jpg";
$missing_path = "images/headshots/missing.jpg";
$full_path = $dirName . "/" . $relative_path;
//if(file_exists($relative_path))
//if(file_exists($_FILES['uploadedfile']['name']))
if(basename( $_FILES['uploadedfile']['name']) != '' /*and (file_exists($_FILES['uploadedfile']['name']))*/)
{
$this->process_headshot_file($relative_path, $full_path);
}
else
{
//$this->process_headshot_file($missing_path, $full_path);
$query = "UPDATE hraps SET headshot_filename = '".$_SESSION['missing_headshot_image']."' WHERE id = ".$this->id;
$result = mydb::cxn()->query($query);
}
Thank you in advance for your help.
Your
ifstatement here:is only going to work when there’s something in
$_FILESSince you say you want to display an image when their information is displayed, that’s probably not going to trigger very often, so the
elseis going off instead.I don’t think you need to do anything more complicated than this: