Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: if file doesn't exist then exit script

Tags:

php

<?php
$filename = '/path/to/foo.txt';

if (file_exists($filename)) {
    echo "The file $filename exists";
}
?>

That script only checks if a file exists, but how do I do something like this?

<?php
$filename = '/path/to/foo.txt';

if (file doesnt exist) {
    exit("Your file doesn't exist");
}
?>
like image 384
Lin Avatar asked Feb 25 '15 00:02

Lin


1 Answers

As John Conde said, you should negate the file_exists function:

if (!file_exists($filename)) {
    exit("Your file doesn't exist");
}
like image 121
Patrick Bard Avatar answered Sep 28 '22 00:09

Patrick Bard