Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how can i know if a photo is available  or not before puting it?

Posted on 2008-10-14
8
173 Views
Last Modified: 2009-07-29
i have 30 photo links that some of them dont work the photo is not there so when i try to see it like this

  <img src="<?php echo $photo[$p];?>"/></br>
i get a x on the photo like its unavailable

so how can i know that its unavailable before and not showing it?
0
Comment
Question by:mgtm3
  • 3
  • 3
  • 2
8 Comments
 
LVL 24

Accepted Solution

by:
glcummins earned 350 total points
ID: 22716190
You can use get_headers() to determine if a URL returns a valid response before using it:

<?php
 
list($response_code) = get_headers($photo[$p]);
 
if (stristr($response_code, '200 OK'))
{
   // The photo exists
   echo $photo[$p];
}
else
{
   // Take whatever action is necessary, like outputting a placeholder image URL.
}
?>

Open in new window

0
 

Author Comment

by:mgtm3
ID: 22716318
is there any faster code?
0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 150 total points
ID: 22716452
If the images are on the local server, you can use file_exists().

You can use a placeholder image, like glcummins suggests, or remove the image completely, i.e. only display the image element if the image actually exists:
<?php if(file_exists($photo[$p]))
  echo '<img src="'.$photo[$p].'"/></br>';
?>

Open in new window

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:mgtm3
ID: 22716538
but the image is not on the sever and the command that glucummis takes for ever
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22716605
It's probably the network and/or the remote server that is slowing you down. There is no way around this. Some packets have to travel across the network to determine if the file exists, and the remote server will use some time to respond. You should of course remove links to non-existing photos from your page, that would solve the problem.
0
 

Author Comment

by:mgtm3
ID: 22716660
how can i remove the links? you mean not showing the image
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22716741
With links I meant "photo links" or image URLs. How did they get into $photo[$p] ? They are probably read from some database table? You should delete or otherwise 'block' these records from that table.
0
 
LVL 24

Expert Comment

by:glcummins
ID: 22717176
I run a large server with many millions of image references to other servers. I run a process in the background that periodically checks for the existence of those images, and updated a database record if they do not exist.

When the customer loads the website, my front-end scripts instantly know if the remote image is available, and do not need to run the slow get_headers() command in real-time.

Perhaps a similar system will work for you?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question