简体   繁体   English

获取从1到5的文件名

[英]Get file names from 1 to 5

The title is not more clear but the question container is more clear. 标题不太清晰,但问题容器更清晰。

I have this code: 我有以下代码:

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

I want to check if the following files ( file1.txt - file2.txt - file3.txt - file4.txt - file5.txt ) exists (but with a simple code) .. So what should be I_WANT_HELP_ON_THIS ? 我想检查以下文件(file1.txt-file2.txt-file3.txt-file4.txt-file5.txt)是否存在(但使用简单的代码)..那么I_WANT_HELP_ON_THIS应该是什么

I don't want to use: 我不想使用:

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

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

etc...

Thanks! 谢谢!

Use logical OR || 使用逻辑或|| operator to get a single condition: 运算符获得一个条件:

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");
}

you could solve this by using an array where you define all files (filenames) which you would like to check and then loop over that array like so 您可以通过使用数组来解决此问题,在数组中定义要检查的所有文件(文件名),然后像这样遍历该数组

$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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM