TarranJones TarranJones - 5 months ago 7
PHP Question

PHP only allow full path with is_file

I want to find if a file exists but only if the full file path if provided.

$file = "C://dir/dir/file.txt"; // should return true

$file = "file.txt" //should return false;

Both files exist, in fact they are the same file.

function is_absolute_file($file){

return is_file($file) && strpos($file, ":". DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR) === 1 ;

Although this does work I would have to do a
on all slashed to make sure they match

There must be a better way. any ideas ?


You can use realpath() to generate the absolute path and then compare it.

function is_absolute_path( $path ) {
  return ( $path == realpath( $path ) );

If the provided path already is an absolute path it should be identical to the path generated by realpath().