Reputation: 111
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
.
Upvotes: 2
Views: 20452
Reputation: 11
Use onerror attribute in img tag
<img onerror="this.src= 'img/No_image_available.png';" src="<?php echo $row['column_name ']; ?>" />
Upvotes: 0
Reputation:
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";
}
Upvotes: 4
Reputation: 839
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?>
Upvotes: 0
Reputation: 2734
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" />
Upvotes: 0
Reputation: 164775
<?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) ?>">
Upvotes: 0
Reputation: 966
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">';
}
?>
Upvotes: 0
Reputation: 16
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.
Upvotes: 0