用file_exists获取哪个文件不存在,但使用一个数组。

问题描述 投票:0回答:2

好吧

我有一个脚本,可以检查数组中的文件是否存在,但现在我不知道如何从不存在的数组中获取文件。

但现在我不知道如何从不存在的数组中获取文件。

<?php
$files = [
   "user/important.ini",
   "user/really_needed.php"
];

if(file_exists($files) == false) {
   $data = "1";
   $meta = array(
       "file" => ????,
       "error" => "Missing file"
    );

?>

所以我想用不存在的文件来代替"???",因为我不知道如何获取这个文件,就是那些问号。

有没有可能的代码,我可以使用,得到不存在的文件?

php function web file-exists
2个回答
2
投票

file_exists() 如果你使用一个数组(就像你现在做的那样),你应该得到一个警告... ...

Warning: file_exists() expects parameter 1 to be a valid path, array given

这假设你想要所有不存在的文件,并保留一个失败的列表。 它使用 foreach() 上的数组,并测试每个项目,如果它不存在,则使用 $meta[] (别忘了在循环之前初始化这个数组) ... ...

$files = [
        "user/important.ini",
        "user/really_needed.php"
];
$meta = [];
foreach ( $files as $file ) {
    if(file_exists($file) == false) {
        $data = "1";
        $meta[] = array(
                "file" => $file,
                "error" => "Missing file"
        );
    }
}

print_r($meta);

1
投票

循环检查文件是否存在是一种方法。我建议使用 foreach 这里的循环。

<?php
$files = [
   "user/important.ini",
   "user/really_needed.php"
];

$meta = [];
foreach ($files as $file) {
   if(!file_exists($file) {
      $data = "1";
      $meta[] = array(
         "file" => $file,
         "error" => "Missing file"
      );
?>
© www.soinside.com 2019 - 2024. All rights reserved.