php check if file exists if not create

Many times we got requirement of check file from specific directory. PHP tutorial for, php check if file exists if not create. Following code will work in all the system like (Mac, Linux and Windows). we given code snippet for checking file exists or not.

php check if file exists if not create

php check if file exists if not create

Using following php code you can check file is present or not in specific directory.


<?php
 $filename = '/var/www/html/test/test.txt';

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

?>

Add a Comment

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