Q: Simple test for file_exist fails Simplest bit of test code:-
<?php
if (file_exists('https://mywebsite/testarea/test.html')) {
    echo 'File exists';
} else {
    echo 'Not found';
}   
?>

I run this test from my localhost (wamp). Why does this not find the file?  I've double checked that it exists in the path specified. Help please.

A: You are passing a file's url that is hosted somewhere else on a remote server rather than on your computer that's why it's not able to locate it.
If you have the same file in your localhost folder or anywhere on your computer where wamp has read access, it will pass through the check smoothly.
However, if you want to check if a specific url exists, then you might want to have a look at get_headers function:
$headers = get_headers('https://mywebsite/testarea/test.html');

if($headers && strpos($headers[0], 200)) {
    echo 'URL exists';
} else {
    echo 'URL does not exist';
}


A: You need to use Path to the file or directory . See file_exists manual : https://www.php.net/manual/en/function.file-exists.php
and for file functions wrapper support : https://www.php.net/manual/en/wrappers.php
For your case something like,
<?php
 if (file_exists('./testarea/test.html')) {
  echo 'File exists';
  } else {
 echo 'Not found';
 }   
?>


On windows, use //computername/share/filename or \computername\share\filename to check files on network shares.


A: You can use file_get_contents()
if (file_get_contents('https://mywebsite/testarea/test.html')) {
    echo 'File exists';
} else {
    echo 'Not found';
}

Or just built this with the help of How to check if a file exists from a url
$ch = curl_init('https://mywebsite/testarea/test.html');
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($code == 200) {
    echo 'File exists';
} else {
    echo 'Not found';
}


A: file_exists() checks whether a file or directory exists in same system where your script is running.
<?php
   $filename = '/path/to/foo.txt';

   if (file_exists($filename)) {
      echo "The file $filename exists";
   } else {
      echo "The file $filename does not exist";
   }
?>

If you are look for a file in remote place via http than use get_header()
<?php
   $url = 'https://mywebsite/testarea/test.html';
   $array = get_headers($url);
   $string = $array[0];
   if(strpos($string,"200")) {
      echo 'url exists';
   } else {
      echo 'url does not exist';
   }
?>

