如何检查文件夹中是否存在PHP中的某个文件
我混淆了:S与此脚本…如果存在一些文件,此脚本将正常工作,我将得到如下回音:如何检查文件夹中是否存在PHP中的某个文件,php,file,Php,File,我混淆了:S与此脚本…如果存在一些文件,此脚本将正常工作,我将得到如下回音: |test1.txt|test2.txt|test3.txt|test4.txt 问题是,当不存在任何文件时,php将出现错误: Warning: asort() expects parameter 1 to be array, null given in htdocs\test\ReadFolder8.php on line 6 Warning: Invalid argument supplied for for
|test1.txt|test2.txt|test3.txt|test4.txt
问题是,当不存在任何文件时,php将出现错误:
Warning: asort() expects parameter 1 to be array, null given in htdocs\test\ReadFolder8.php on line 6
Warning: Invalid argument supplied for foreach() in htdocs\test\ReadFolder8.php on line 8
有没有可能让一个函数查看文件是否存在,如果没有,会得到一个类似“未找到任何文件”的回音,而不会出现错误
<?php
$User = $_GET['User'];
foreach (glob("Myuser/$User/*.txt") as $path) {
$docs[$path] = filectime($path);
} asort($docs); // sort by value, preserving keys
foreach ($docs as $path => $timestamp) {
//print date("d M. Y:|H:i:s |", $timestamp);
print '|'. basename($path) .'' . "" . '';
}
?>
最后一件事,我不明白文件的数据创建顺序是如何工作的。。。如果我创建一个新文件,新文件将是字符串的最后一个…我如何颠倒顺序
非常感谢:解决方案很简单:您需要先初始化变量$docs,然后再将其使用为空变量,如下所示:
$docs = array(); // <-- create array $docs before use...
foreach (glob("Myuser/$User/*.txt") as $path) {
$docs[$path] = filectime($path);
}
asort($docs); // sort by value, preserving keys
if (count($docs) == 0) {
echo "no file found";
}
您可以使用arsort而不是asort来获取最新的文件作为第一个文件。为错误案例处理添加了一些代码/解释。当然,您也可以使用if!文件_existsglobMyuser/$User/{….error…}以检查目录是否存在。请注意,文件_的存在也适用于目录。谢谢!!它工作得很好!!最后一个问题是…如何按日期排序文件?最新的文件它是最后一个。。。再次感谢!!