Breaking News
Home / Web / PHP / PHP Snippets / How to check if file exists in PHP?

How to check if file exists in PHP?

Snippet
Using the file_exists function it is very easy to check whether a file exists in a given directory or not. Say we want to check if file name audio.mp3 exists or not in /tmp folder.

  1. <?php
  2. $filename = ‘/tmp/audio.mp3’;
  3.  
  4. if (file_exists($filename)) {
  5. echo "The file $filename exists";
  6. } else {
  7. echo "The file $filename does not exist";
  8. }
  9. ?>
[pb_builder]

Check Also

How to list files in a directory in PHP?

<?php if ($handle = opendir(‘/path/to/directory’)) {     echo "Directory handle: $handlen";     echo …

Advertisment ad adsense adlogger