-1

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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
oldcelt
  • 1
  • 4
  • 2
    Tip As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality. – AbraCadaver Jul 08 '20 at 15:02

4 Answers4

0

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';
}
Tushar
  • 665
  • 5
  • 11
0

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 l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0

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';
}
Nipun Tharuksha
  • 2,496
  • 4
  • 17
  • 40
  • Thanks to everybody for all the useful help. Will have another gp tomorrow using the advice given. – oldcelt Jul 08 '20 at 16:32
0

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';
   }
?>
mail2bapi
  • 1,547
  • 1
  • 12
  • 18