PHP file_exists(), filesize() and realpath() functions



PHP file_exists(), filesize() and realpath() file handling

In this php tutorial we will learn how use file_exists() function, filesize() function and realpath() function with php example.

PHP File handling functions :

  • file_exists() – used to check whether the file exists or not.
  • filesize() – used to check size of file.
  • realpath() – used to check real path of file.

PHP file_exists() Function

The file_exists() function is used to check whether the file or directory exists or not.

file_exists() function syntax

<?php

file_exists(“filename”);

// for check directory
file_exists(“directory name”);

?>

file_exists() function example

<?php

//check file existence
if(file_exists("abc.txt"))
{
echo "file available";
}
else
{
echo "file not available";
}

?>

In above php file_exists() example if the file ‘abc.txt’ is exists then display message like ‘file available’ other wise display message ‘file not available’.


PHP filesize() Function

The filesize() function is used to check file size.

filesize() function syntax

<?php

filesize(“filename”);

?>

filesize() function example

<?php

echo filesize("abc.txt");

?>

PHP realpath() Function

The realpath() function is used to check real path of file.

realpath() function syntax

<?php

realpath(“filename”);

?>

realpath() function example

<?php

echo realpath("abc.txt");

?>

The output of php example:

C:\wamp\www\abc.txt

Leave a Reply

Your email address will not be published. Required fields are marked *