【发布时间】:2017-05-16 07:21:41
【问题描述】:
我想检查一个目录是否包含特定文件(任何类型、图像、pdf 等)。如果它包含该文件,我想提供该文件的下载链接,否则打印"not exist"。不幸的是,我的代码一直在打印not exist,即使该文件存在于目录中。这是我的代码:
<td> <?php
if (file_exists('plans/'.$RID)) {
echo"<a href='plans/$RID'>Plan</a>";
} else {
echo "not exists";
}
?> </td>
这是另一个我也试过但不起作用的代码:
<td> <?php
$plan= 'plans/'.$RID;
if (file_exists($plan)) {
echo"<a href='plans/$RID'>Plan</a>";
} else {
echo "not exists";
}
?> </td>
【问题讨论】:
-
将绝对路径传递给
file_exists -
绝对路径是什么意思
-
类似
PROJECT_ROOT . "/plans/$RID"(/path/to/plans/1234) -
可能是工作目录问题。使用 if (file_exists('../plans/'.$RID))
-
我添加了绝对路径,但它仅在我指定文件扩展名时才有效(例如 if (file_exists('../plans/'.$RID.'.jpg')))。问题是我不想指定扩展名,因为文件类型不同。我只想要名称为 $RID 的文件,无论其扩展名是什么
标签: php html file-exists