By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,348 Members | 1,397 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,348 IT Pros & Developers. It's quick & easy.

How to find out if a file exists on a remote / local server? file_exists(), fopen()?

100+
P: 113
Hi there,

This has probably been asked a million times but having looked through the PHP manual for the file_exists() function I can't seem to get the right code for what I want to do.

I have a typical scenario of wanting to check for an image files existence before displaying, I have foubd the following code to do this but it always returns false:

Expand|Select|Wrap|Line Numbers
  1. $imageURL = "http://website.com/images/image1.jpg";
  2.  
  3. $handle = @fopen($imageURL, 'r');
  4. if ($handle===false) { 
  5.   echo "image does not exist";
  6. }
  7. else {
  8.   echo "image exists";
  9.   fclose($handle);
  10. }
  11.  
What is the best way to find out if an image exists on a remote or local server?

Thanks,

Chromis
Feb 19 '08 #1
Share this Question
Share on Google+
5 Replies


P: 4
hi Chromis,

PHP file_exists() function takes file system path. as an argument.
Feb 19 '08 #2

100+
P: 113
hi Chromis,

PHP file_exists() function takes file system path. as an argument.
Hi abhishektiwari,

Thanks for the reply, I wish to check if a file exists using an http address, i am thinking file_exists() is not the right function to use, what function should I use?

Thanks,

Chromis
Feb 19 '08 #3

nomad
Expert 100+
P: 664
Hi abhishektiwari,

Thanks for the reply, I wish to check if a file exists using an http address, i am thinking file_exists() is not the right function to use, what function should I use?

Thanks,

Chromis
yes you can use file_exist

might look something like this


if (file_exists("test.txt")) {
echo "test.txt is a file!";
}


nomad
Feb 19 '08 #4

Markus
Expert 5K+
P: 6,050
Hi there,

This has probably been asked a million times but having looked through the PHP manual for the file_exists() function I can't seem to get the right code for what I want to do.

I have a typical scenario of wanting to check for an image files existence before displaying, I have foubd the following code to do this but it always returns false:

Expand|Select|Wrap|Line Numbers
  1. $imageURL = "http://website.com/images/image1.jpg";
  2.  
  3. $handle = @fopen($imageURL, 'r');
  4. if ($handle===false) { 
  5.   echo "image does not exist";
  6. }
  7. else {
  8.   echo "image exists";
  9.   fclose($handle);
  10. }
  11.  
What is the best way to find out if an image exists on a remote or local server?

Thanks,

Chromis
file_exists() is perfect to use! Is it not obvious? You're wanting to check if a file exists ... file_exists?

;)
Feb 19 '08 #5

100+
P: 113
file_exists() is perfect to use! Is it not obvious? You're wanting to check if a file exists ... file_exists?

;)
Yeah it's completely obvious but what is perhaps not so obvious in my explanation is that I want to find out whether a file at a specific URL exists. file_exists() in PHP 5 does not accept URLs only local path names.

Having tryed a number of different solutions only this one worked for me:

Expand|Select|Wrap|Line Numbers
  1.     function url_exists($url){
  2.         $url = str_replace("http://", "", $url);
  3.         if (strstr($url, "/")) {
  4.             $url = explode("/", $url, 2);
  5.             $url[1] = "/".$url[1];
  6.         } else {
  7.             $url = array($url, "/");
  8.         }
  9.  
  10.         $fh = fsockopen($url[0], 80);
  11.         if ($fh) {
  12.             fputs($fh,"GET ".$url[1]." HTTP/1.1\nHost:".$url[0]."\n\n");
  13.             if (fread($fh, 22) == "HTTP/1.1 404 Not Found") { return FALSE; }
  14.             else { return TRUE;    }
  15.  
  16.         } else { return FALSE;}
  17.     }    
  18.  
Thanks for your input though,

Chromis
Feb 19 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.