Hello davidphp-ga,
Thank-you for your question.
There are several methods you can use to check whether a file exists
or not. shnads-ga has given you one example below and I thought I
would supply you with a further example.
First, shnads-ga example using fopen:
"fopen() binds a named resource, specified by filename, to a stream.
If filename is of the form "scheme://...", it is assumed to be a URL
and PHP will search for a protocol handler (also known as a wrapper)
for that scheme. If no wrappers for that protocol are registered, PHP
will emit a notice to help you track potential problems in your script
and then continue as though filename specifies a regular file.
If PHP has decided that filename specifies a local file, then it will
try to open a stream on that file. The file must be accessible to PHP,
so you need to ensure that the file access permissions allow this
access. If you have enabled safe mode, or open_basedir further
restrictions may apply.
If PHP has decided that filename specifies a registered protocol, and
that protocol is registered as a network URL, PHP will check to make
sure that allow_url_fopen is enabled. If it is switched off, PHP will
emit a warning and the fopen call will fail."
http://uk.php.net/manual/en/function.fopen.php
There are also further examples of other methods of checking whether a
file exists on the thread at the above web link.
Combining your code with shnads-ga's code you would get something like this:
$handle = @fopen('http://www.greathostels.com/pics/$image_path', 'r');
if($handle) && $image_path != ''){
...do this...
};
I noticed that your question appears to be related to images in some
way which suggested to me my solution of how to do this. PHP 4.0.5+
has a function called getimagesize which allows you to use a URL when
asking for the details of an image.
"The getimagesize() function will determine the size of any GIF, JPG,
PNG, SWF, SWC, PSD, TIFF, BMP, IFF, JP2, JPX, JB2, JPC, XBM, or WBMP
image file and return the dimensions along with the file type and a
height/width text string to be used inside a normal HTML <IMG> tag.
If accessing the filename image is impossible, or if it isn't a valid
picture, getimagesize() will return FALSE and generate an error of
level E_WARNING."
http://uk.php.net/manual/en/function.getimagesize.php
You could therefore use some code like this to check whether the image
you are looking for exists:
if ( getimagesize('http://www.greathostels.com/pics/$image_path') &&
$image_path != '') {
...do this...
else {
...otherwise do this...
};
If you are using images this method provides some useful information
on the image you are trying to check is there - such as the dimensions
of the image - which may be useful elsewhere in your script.
If you require any further assistance on this subject please do not
hesitate to ask for clarification. |