I'm trying to scan the files in a directory using a php request. This is my code right now:
$.ajax({
type: "POST",
url: "scandir.php",
data: { dirname: "./test" },
success: function( data ) {
console.log( data );
}
error: function() {
console.log( "error" );
}
});
scandir.php:
<?php
$dirname = $_POST[ "dirname" ];
$files = scandir( $dirname ) or die("Unable to scan directory " . $dirname );
foreach ( $files as $filename ) {
echo $filename . "
";
}
?>
Problem is, the request always returns successfully, even if the directory "dirname" doesn't exist (it returns the text of the php directory-not-found messages). How do I make it return with an error when the directory is not found?