[英]Downloadscript > Do not display files with a specific extension
Donloadscript > Do not display files with a specific extension. Donloadscript > 不显示具有特定扩展名的文件。
I have a script for downloads in which the following instruction does not show certain files.我有一个下载脚本,其中以下说明未显示某些文件。 This also works very well.
这也很有效。
while ($file_name = readdir($dir_handle)) {
// Hide files so only visible files can be downloaded
if ($file_name != '.access' and $file_name != '.' and $file_name != '.htaccess' and $file_name != 'index.php' and $file_name != '001.php' and $file_name != '002.php' and $file_name != '003.php' and $file_name != '005.php' and $file_name != '007.php' and $file_name != '009.php' and $file_name != '010.php') {
$show_files
}
Question: How to define all PHP files globally instead of having to explicitly define each PHP file like in the posted code?问题:如何在全局范围内定义所有 PHP 文件,而不是像在发布的代码中那样显式定义每个 PHP 文件? It doesn't work with
$file_name.= '*.php
它不适用于
$file_name.= '*.php
Many thanks in advance for tips and hints非常感谢您的提示和提示
Since PHP 8 we have str_ends_with
which should work for you.由于 PHP 8 我们有
str_ends_with
应该适合你。 That, along with a lookup array for literal files should be enough.那,连同文字文件的查找数组就足够了。 See this for a version of
str_ends_with
for older versions of PHP, although I'd recommend the Symfony polyfill potentially, too, just to have access to other functions.有关旧版本 PHP 的
str_ends_with
版本,请参阅此版本,尽管我也可能推荐Symfony polyfill ,只是为了访问其他功能。
function shouldFileBeHiddenA(string $file): bool {
static $knownHiddenFiles = ['.access', '.', '.htaccess'];
if(in_array($file, $knownHiddenFiles)) {
return true;
}
if(str_ends_with($file, '.php')) {
return true;
}
return false;
}
An alternative is to use the pathinfo
function which is probably overkill but might read easier for some people:另一种方法是使用
pathinfo
function 这可能有点矫枉过正,但对某些人来说可能更容易阅读:
function shouldFileBeHiddenB(string $file): bool {
static $knownHiddenFiles = ['.access', '.', '.htaccess'];
static $knownHiddenExtensions = ['php'];
if(in_array($file, $knownHiddenFiles)) {
return true;
}
if(in_array(pathinfo($file, PATHINFO_EXTENSION), $knownHiddenExtensions)) {
return true;
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.