【问题标题】:Get file names from 1 to 5获取文件名从 1 到 5
【发布时间】:2017-02-05 16:29:51
【问题描述】:

标题不是更清晰,但问题容器更清晰。

我有这个代码:

if (!file_exists(**I_WANT_HELP_ON_THIS**)) {
    header("Location: error.php");
}

我想检查以下文件( file1.txt - file2.txt - file3.txt - file4.txt - file5.txt )是否存在(但使用简单的代码).. 那么应该是什么 I_WANT_HELP_ON_THIS ?

我不想使用:

if (!file_exists('file1.txt')) {
    header("Location: error.php");
}

if (!file_exists('file2.txt')) {
    header("Location: error.php");
}

etc...

谢谢!

【问题讨论】:

    标签: php


    【解决方案1】:

    使用逻辑 OR || 运算符获取单个条件:

    if (!file_exists('file1.txt') || !file_exists('file2.txt') 
        || !file_exists('file3.txt') || !file_exists('file4.txt') || !file_exists('file5.txt')) {
        header("Location: error.php");
    }
    

    【讨论】:

    • 我也是这么想的,但是如果有很多文件,我建议使用数组方法,因为它更具可读性。还是我错了?
    • @flynorc,我们有确切的标题:获取从 1 到 5 的文件名。否则,应该详细说明问题
    • @flynorc,没关系
    【解决方案2】:

    您可以通过使用一个数组来解决这个问题,在该数组中定义您想要检查的所有文件(文件名),然后像这样遍历该数组

    $files = array("file1.txt", "file2.txt",...);
    foreach($files as $file) {
        if(!file_exists($file) {
            header("Location: error.php");
            exit(); //or just break; if you still need to do something else
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-17
      • 2015-06-13
      • 2011-04-08
      • 2010-10-10
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多