【发布时间】:2013-12-14 09:33:45
【问题描述】:
我有数据库列 attach1 和 attach2。 我需要显示这些列中的文件(pdf),但前提是它们存在于目录 www.domain.com/uploads 中。 Attach1 包含真实文件,但 attach2 不包含。
我尝试过这样的事情:
<?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';
}
?>
但每次它回复我时,该文件都存在,即使 attach2 不包含任何内容。为什么?
【问题讨论】:
-
不是你的问题,但你的代码示例中有许多毫无意义的东西。您不需要将 ampty 字符串连接到文件名的末尾。您不需要将
file_exists的结果保存在变量中。您可以使用else来简化条件。 -
您是否尝试
echo $file2并确保它不为空?如果是,您将检查file_exists('/uploads'),当然是TRUE。 -
试试这个'if(isset($file)'
-
是的,我确定它是空的。好的,如果我正在检查目录,如何检查具体文件?