如何检查路径是PHP中的文件还是文件夹 [英] How to check a path is a file or folder in PHP

查看:74
本文介绍了如何检查路径是PHP中的文件还是文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用scandir()递归搜索文件.但是,如果文件路径指向文件而不是文件夹,则会出现警告.如何检查路径是直接引导文件还是文件夹?

I use scandir() to search files recursively. But if the file path directs to a file not a folder, there will be a warning. How can I check the path whether it directs a file or folder?

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()

Have a look into is_dir() and is_file()

<?php

$path = "C:\\test_folder\\folder2\\folder4";
$sub_folder = scandir($path);
$num = count($sub_folder);
for ($i = 2; $i < $num; $i++)
{
    if(is_file($path.'\\'.$sub_folder[$i])){
        echo 'Warning';
    }

}
?>

这篇关于如何检查路径是PHP中的文件还是文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆