I have database columns attach1 and attach2. I need to show files (pdf) from those columns, but only if they exist in directory www.domain.com/uploads. Attach1 contains real file but attach2 does not.
I tried something like this:
<?php
$file = $row['attach'];
$exists = file_exists('uploads/'.$file.'');
if ($exists) {
echo $file;
}
if (!$exists) {
echo 'No file1';
}
?>
<?php
$file2 = $row['attach2'];
$exists = file_exists('uploads/'.$file2.'');
if ($exists) {
echo $file2;
}
if (!$exists) {
echo 'No file2';
}
?>
But everytime it echoes me back, that file exists, even when attach2 contains nothing. Why?
file_existsin a variable. You can useelseto simplify your conditionals.echo $file2and make sure it's not empty? If it is, you'd be checkingfile_exists('/uploads'), which is of courseTRUE.