if <img src=""> but no image - php

When trying to display image that's adress doesn't exist you will see default browsers "no image" badge.
How to change it to default "no-image.png" placeholder?
echo "<img src='http://www.google.com/trolol.png'>"; //for example

You can use the onerror attribute, example:
<img src="" onerror="this.src = 'no-image.png';" alt="" />
Demo: http://jsfiddle.net/kNgYK/

<img src='http://www.google.com/trolol.png' onerror="this.src='http://jsfiddle.net/img/logo.png'">​
sample http://jsfiddle.net/UPdZh/1/
The onerror event is triggered if an error occurs while loading an
external file (e.g. a document or an image).
Syntax
<element onerror="SomeJavaScriptCode">
http://wap.w3schools.com/jsref/event_onerror.asp

<?php
$filename = 'http://www.google.com/trolol.png';
if (file_exists($filename)) {
echo "<img src='".$filename."'>";
} else {
echo "<img src='no-image.png'>";
}
?>

Try this
<?php
$filename = 'http://www.google.com/trolol.png';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>

As you asked for a PHP solution:
if (file_exists($filename)) {
echo "i_exist.jpg";
} else {
echo "fallback.jpg";
}
IMPORTANT: Works only for local files, not for remote ones! Thanks to Szymon for the comment!

Related

How do I get PHP to display a user picture if you have one within a blog post?

I have a script that can disply user pictures but I want to have it now so if you don't have a picture it displays a default one.
CODE
if (file_exists($displayuserimage)) {
$displayuserimage = '<img src="upic/'. $posts['userid'].'.jpg" width="50px" height="50px" border="2" />';
} else {
$displayuserimage = '<img src="upic/default.jpg" width="50px" height="50px" border="2"/>';
}
That's what im guessing, but this is the working script, that shows images if you have one:
$displayuserimage = '<img src="upic/'. $posts['userid'].'.jpg" width="50px" height="50px" border="2"/>';
Your if is checking a variable,$displayuserimage, which has not been defined before. To check if your image exists, that if may look like
if (file_exists("upic/". $posts['userid'].".jpg")) {
Using PHP's file_exists() function
http://php.net/manual/en/function.file-exists.php
$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";
}
Use the same scenario if you are calling user images from Database too. Replace the
echo "<img src=\"$http_image\"/>\n";
with your row from the Database which contains your user's profile image. Example:
echo $rows['user_image'];

delete image from folder PHP

I have a folder where I keep my images, named img/. I have a table with all of my images:
<table border="3">
<tr>
<td>
<?php
$files = glob("img/*");
foreach ($files as $file) {
echo "<div class='divimages'>";
echo '<img src="'.$file.'"/>';
echo "<input type='submit' value='Delete image'/><br>";
echo "</div>";
}
?>
</td>
</tr>
</table>
How can I delete the image associated to the button with the value:"Delete image".
There are a few routes. One, the most simple, would involve making that into a form; when it submits you react to POST data and delete the image using unlink
DISCLAIMER: This is not secure. An attacker could use this code to delete any file on your server. You must expand on this demonstration code to add some measure of security, otherwise you can expect bad things.
Each image's display markup would contain a form something like this:
echo '<form method="post">';
echo '<input type="hidden" value="'.$file.'" name="delete_file" />';
echo '<input type="submit" value="Delete image" />';
echo '</form>';
...and at at the top of that same PHP file:
if (array_key_exists('delete_file', $_POST)) {
$filename = $_POST['delete_file'];
if (file_exists($filename)) {
unlink($filename);
echo 'File '.$filename.' has been deleted';
} else {
echo 'Could not delete '.$filename.', file does not exist';
}
}
// existing code continues below...
You can elaborate on this by using javascript: instead of submitting the form, you could send an AJAX request. The server-side code would look rather similar to this.
Documentation and Related Reading
unlink - http://php.net/manual/en/function.unlink.php
$_POST - http://php.net/manual/en/reserved.variables.post.php
file_exists - http://php.net/manual/en/function.file-exists.php
array_key_exists - http://php.net/manual/en/function.array-key-exists.php
"Using PHP With HTML Forms" - http://www.tizag.com/phpT/forms.php
You can delete files in PHP using the unlink() function.
unlink('path/to/file.jpg');
First Check that is image exists? if yes then simply Call unlink(your file path) function to remove you file otherwise show message to the user.
if (file_exists($filePath))
{
unlink($filePath);
echo "File Successfully Delete.";
}
else
{
echo "File does not exists";
}
For deleting use http://www.php.net/manual/en/function.unlink.php
Hope you'll can to write logic?
You can try this code. This is Simple PHP Image Deleting code from the server.
<form method="post">
<input type="text" name="photoname"> // You can type your image name here...
<input type="submit" name="submit" value="Delete">
</form>
<?php
if (isset($_POST['submit']))
{
$photoname = $_POST['photoname'];
if (!unlink($photoname))
{
echo ("Error deleting $photoname");
}
else
{
echo ("Deleted $photoname");
}
}
?>
<?php
require 'database.php';
$id = $_GET['id'];
$image = "SELECT * FROM slider WHERE id = '$id'";
$query = mysqli_query($connect, $image);
$after = mysqli_fetch_assoc($query);
if ($after['image'] != 'default.png') {
unlink('../slider/'.$after['image']);
}
$delete = "DELETE FROM slider WHERE id = $id";
$query = mysqli_query($connect, $delete);
if ($query) {
header('location: slider.php');
}
?>
<?php
$path = 'img/imageName.jpg';
if (is_file($path)) {
unlink($path);
} else {
die('your image not found');
}

Php image show issue

I'm confused! Basically i've a form with 3 field ex: subject, image, message. So,
(1) If i upload image it's should be show image,
(2) if i don't upload image it's should be show just only subject and message no image box and
(3) if i don't upload image and if there are a youtube link in message box then it's should be show the vedio. **
Following is my php code, but i can't solved the number 3 point! **
<?php
if(empty($imgname))
{
echo '';
}
elseif(!empty($imgname))
{
echo "<span class='vedio'>";
echo '<img src="' . $upload_path . '/' . $imgname . '" width="195" height="130"
style=" background-color:f2f4f6;" />';
echo "</span>";
}
else
{
echo "<span class='vedio'>";
require_once("func.php");
if (preg_match('%(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)
([^"&?/ ]{11})%i', $msg, $match))
{
// echo $video_id = $match[1];
}
echo #get_youtube_embed($video_id = $match[1]);
echo "</span>";
}
?>
Any Idea or Solution that would be better for me.
Shibbir.
You'll never get into your else statement because of your preceding if conditions. In ALL cases, $imgname will either be empty, or not empty.
To demonstrate further:
if (empty($imgname)) {
// ... First condition
} else if (!empty($imgname)) {
// ... Last possible condition
} else {
// Will never reach here because $imgname will
// fall into one of the two of the above conditionals.
}
It's difficult to add to your code without full context, but it seems you may want to include the following after the above code (not with):
if(!empty($msg) && preg_match('%(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)
([^"&?/ ]{11})%i', $msg, $match)) {
require_once("func.php");
echo "<span class='vedio'>";
echo #get_youtube_embed($match[1]);
echo "</span>";
}

PHP: if image exists show image else show different image

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.
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";
}
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.
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">';
}
?>
<?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) ?>">
You could use http://us3.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" />
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?>
Use onerror attribute in img tag
<img onerror="this.src= 'img/No_image_available.png';" src="<?php echo $row['column_name ']; ?>" />

php - Decide whether an image exist and if it does show it

I have a code that shows a different image depending where on the page I am, but some places don't have an image so it displays a "no image" icon. I want to add a condition that checks if there really is an image in the given path and if returns false don't do anything. I have no idea how to do it.
This is the original code:
<?php
$search=get_search_query();
$first=$search[0];
if ($first=="#"){
echo "<html>";
echo "<img src='http://chusmix.com/Imagenes/grupos/".substr(get_search_query(), 1). ".jpg'>";
}
?>
What I need to know is which function do I use to get a true/false of that image path. Thanks
Use file_exists
$image_path = 'Imagenes/grupos/' . substr(get_search_query(), 1) . '.jpg';
if (file_exists($image_path)) {
echo "<img src='http://chusmix.com/Imagenes/grupos/".substr(get_search_query(), 1). ".jpg'>";
} else {
echo "No image";
}
http://php.net/manual/en/function.file-exists.php
You can use file_exists

Categories