Why doesn't my PHP function work as expected?

Php programming file_exists syntax

  • 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/%24image_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:

    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/%24image_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/%24image_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 at Google Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.