Google Answers Logo
View Question
 
Q: Php programming file_exists syntax ( Answered 5 out of 5 stars,   2 Comments )
Question  
Subject: Php programming file_exists syntax
Category: Computers > Programming
Asked by: davidphp-ga
List Price: $15.00
Posted: 02 Jun 2006 19:00 PDT
Expires: 02 Jul 2006 19:00 PDT
Question ID: 734892
I need to know the correct code which will return a value from a url
and not just locally.

if(file_exists("http://www.greathostels.com/pics/$image_path") &&
$image_path != ''){
as this doesnt work? i think the file_exists command is not url
friendly. The following code is off the site hosted locally

function returnCustomThumbnail($image_path, $image_quality,
$custom_extension, $width, $height, $default_thumbnail){

	if(file_exists("pics/$image_path") && $image_path != ''){ 
							
		//create a compressed thumbnail using the function
		//from our functions file
		//if it fails, we'll substitute a generic
		//thumbnail_not_available jpeg

		$image_serverpath = "pics/$image_path";

		//echo "image_serverpath is $image_serverpath<br>";
		
		//we're going to split the filename on the last dot so
		//we can throw away the old file extension and add our own
		$exploded_dot_array = explode('.', $image_path);
Answer  
Subject: Re: Php programming file_exists syntax
Answered By: palitoy-ga on 03 Jun 2006 02:05 PDT
Rated:5 out of 5 stars
 
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.
davidphp-ga rated this answer:5 out of 5 stars and gave an additional tip of: $5.00

Comments  
Subject: Re: Php programming file_exists syntax
From: shnads-ga on 03 Jun 2006 00:26 PDT
 
Hey David,
As long as you can access your site's php.ini to set allow_url_fopen
to On you can use fopen, example:

<?php
$handle = @fopen('http://au.php.net/manual/en/function.fopen.php', 'r');
if ($handle) :
echo 'Exists';
else:
echo "Does Not Exist";
endif;
?>
Subject: Re: Php programming file_exists syntax
From: palitoy-ga on 04 Jun 2006 01:34 PDT
 
Thank-you for the 5-star rating and tip.  They are both appreciated.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy