如何在PHP中检查文件或文件夹的路径
我使用scandir()递归搜索文件。但是,如果文件路径指向文件而不是文件夹,则会出现警告。如何检查路径是否指向文件或文件夹如何在PHP中检查文件或文件夹的路径,php,file,scandir,Php,File,Scandir,我使用scandir()递归搜索文件。但是,如果文件路径指向文件而不是文件夹,则会出现警告。如何检查路径是否指向文件或文件夹 enter code here <?php $path = "C:\\test_folder\\folder2\\folder4"; $sub_folder = scandir($path); $num = count($sub_folder); for ($i = 2; $i < $num; $i++) { ...//if $sub_folder[$i]
enter code here
<?php
$path = "C:\\test_folder\\folder2\\folder4";
$sub_folder = scandir($path);
$num = count($sub_folder);
for ($i = 2; $i < $num; $i++)
{
...//if $sub_folder[$i] is a file but a folder, there will be a warning.
How can I check $sub_folder[$i] before use it?
}
?>
在此处输入代码
谢谢 应该告诉您路径是否为目录 查看is_dir()和is_file()
您也可以使用()
此外,您还可以检查和。您可以检查变量是否具有文件扩展名
此“如果”将仅获取文件:
if (pathinfo($file, PATHINFO_EXTENSION))
这个“如果”将只获取目录:
if (!pathinfo($file, PATHINFO_EXTENSION))
一个非常快速的google在php手册中给出了答案,bool is_dir(string$filename)。谢谢!我以前使用的是is_dir()和is_file(),但是错误地使用了$sub_folder[$I]作为参数,没有得到我想要的结果。谢谢!我用了is_fie()。
if (pathinfo($file, PATHINFO_EXTENSION))
if (!pathinfo($file, PATHINFO_EXTENSION))