2

i am making a login system with registration and a profile page in php and i am trying to make a profile picture work.

if the user has not uploaded a profile picture yet then make it show a "no profile picture" image if the user has uploaded a profile picture make make it show the image that he has uploaded.

Right now it only show the default picture, noprofile.png.

< img src="uploads/< ? echo "$username" ? >/noprofile.png">

i want it to show icon.png if icon.png has been uploaded and if it hasnt been uploaded make it show, noprofile.png.

7 Answers 7

4

Just run it through the logic, using file_exists:

$image="/path/on/local/server/to/image/icon.png";
$http_image="http://whatever.com/url/to/image";

if(file_exists($image))
{
  echo "<img src=\"$http_image\"/>\n";
}
else
{
  echo "<img src=\"uploads/$username/noprofile.png\"/>\n";
} 
Sign up to request clarification or add additional context in comments.

Comments

0

Check to see if the file has been uploaded by using file exists. If the file exists, use that url else use the default noprofile.png.

Comments

0

you could make a column in the DB to store a value if it has been uploaded or not.

OR

you could see if the file exists.

<?php
if (file_exists('uploads/' . $username . '/icon.png')) {
echo '<img src="uploads/' . $username . '/icon.png">';
}
else {
echo '<img src="uploads/' . $username . '/noprofile.png">';
}
?>

Comments

0
<?php

$img = file_exists(sprintf('/path/to/uploads/%s/icon.png', $username))
     ? 'icon.png' : 'noprofile.png';
?>

<img src="uploads/<?php printf('%s/%s', htmlspecialchars($username), $img) ?>">

Comments

0

You could use https://www.php.net/file_exists to check if the image file is there.

Another alternative is - assuming you keep your user info in a database - have a column with the image name. Since you have to retrieve info from your user table anyway, check to see if that column is NULL or blank. If it is, the user has not uploaded an image yet.

Then, in the page you display the user photo, you might have code something like this:

$userPhoto = ($photoName)? $photoName : 'placeholder';
echo '<img src="uploads/'.$userPhoto.'.png" />

1 Comment

where $photoName is a string from the user photo column in your DB, or is NULL/false
0

Assuming the filepaths are correct, here's what you do...

<?php $filename = "uploads/".$username;    
$imgSrc = file_exists($filename) ? $filename : "uploads/noprofile.png"; ?>
<img src=<?php echo $imgSrc?>

Comments

0

Use onerror attribute in img tag

<img onerror="this.src= 'img/No_image_available.png';"  src="<?php echo $row['column_name '];  ?>" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.