Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 扫描文件夹中的文件而不是数组_Php_File_Filesystems - Fatal编程技术网

Php 扫描文件夹中的文件而不是数组

Php 扫描文件夹中的文件而不是数组,php,file,filesystems,Php,File,Filesystems,我有以下代码: <?php $allowed = array('file1', 'file2', 'file3'); if (in_array($_GET["url"], $allowed)) { // You can include } else { // Error message and dont include } ?> 但是,我不需要在数组中写入所有文件名,如何才能使例如my folder files/中的文件不被其他文件接受。如何做到这一点?使用如下

我有以下代码:

<?php
$allowed = array('file1', 'file2', 'file3');

if (in_array($_GET["url"], $allowed)) {
    // You can include
} else {
   // Error message and dont include
}
?>

但是,我不需要在数组中写入所有文件名,如何才能使例如my folder files/中的文件不被其他文件接受。如何做到这一点?

使用如下功能:

if (file_exists('FILES/'.basename($_GET["url"]))) {
    // You can include
} else {
   // Error message and dont include
}
使用如下函数:

if (file_exists('FILES/'.basename($_GET["url"]))) {
    // You can include
} else {
   // Error message and dont include
}

用于检索文件夹中文件的函数:

<?
function fGetFilesInFolder($sFolder) {

    $aFiles = array();
    if(file_exists($sFolder)) {
        if ($handle = opendir($sFolder)) {
            while (false !== ($sFile = readdir($handle))) {
                if ($sFile != "." && $sFile != "..") $aFiles[] = $sFile;
            }
            closedir($handle);
        }
    }
    return $aFiles;
}
?>

检索文件夹中文件的功能:

<?
function fGetFilesInFolder($sFolder) {

    $aFiles = array();
    if(file_exists($sFolder)) {
        if ($handle = opendir($sFolder)) {
            while (false !== ($sFile = readdir($handle))) {
                if ($sFile != "." && $sFile != "..") $aFiles[] = $sFile;
            }
            closedir($handle);
        }
    }
    return $aFiles;
}
?>


$\u GET['url']
中小心相对路径!在文件_exists docs中,“检查文件或目录是否存在”。因此,这将允许应用程序尝试包含目录,这将导致错误。在下面的回答中,我还修复了相对路径问题。您仍将包括目录。在
$\u GET['url']
中小心使用相对路径!在文件_exists docs中,“检查文件或目录是否存在”。因此,这将允许应用程序尝试包含目录,这将导致错误。在下面的回答中,我还解决了相对路径问题。您仍将包括目录。@ajreal,这不完全正确;Moussa所做的是安全地做这件事的唯一方法,将可能包含的文件列为白名单。也就是说,这仍然很可怕。哎呀,发错了,我指的是其他人answer@ajreal,这并不完全正确;Moussa所做的是安全地做这件事的唯一方法,将可能包含的文件列为白名单。尽管如此,这仍然非常可怕。哎呀,发错了,我指的是剩下的答案