PHP在子文件夹中添加用于搜索文件的命令

时间:2019-05-07 05:01:04

标签: php

我有一个运行良好的脚本,但是我需要添加一个命令来搜索所有子文件夹。

示例:我有一个文件夹数据,其中包含更多其他文件夹...我需要在此文件夹中搜索文件。

# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/example.com/before/*;

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name .example.com;
    root /home/forge/example.com/current/public;

    client_max_body_size 100M;

    # FORGE SSL (DO NOT REMOVE!)
    ssl_certificate /etc/nginx/ssl/example.com/302491/server.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com/302491/server.key;

    ssl_protocols TLSv1.2;
    # Updated cipher suite per Mozilla recommendation for Modern compatibility
    # https://wiki.mozilla.org/Security/Server_Side_TLS#Modern_compatibility
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/dhparams.pem;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Content-Type-Options "nosniff";
    add_header Vary "Origin";

    add_header Access-Control-Allow-Origin "*";
    add_header Access-Control-Allow-Credentials 'true';
    add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
    add_header Referrer-Policy "strict-origin-when-cross-origin";
    add_header Public-Key-Pins 'pin-sha256="hpkppinhash="; pin-sha256="anotherpinhash="; pin-sha256="yetanotherpinhash="; pin-sha256="anotherpinhash="; pin-sha256="lastpinhash="; max-age=86400';


    index index.html index.htm index.php;

    charset utf-8;

    # FORGE CONFIG (DOT NOT REMOVE!)
    include forge-conf/example.com/server/*;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off; 
    error_log  /var/log/nginx/example.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    location ~* \.json {
    add_header Cache-Control "no-store,no-cache";
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
        add_header Referrer-Policy "strict-origin-when-cross-origin";
    }
}

# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/example.com/after/*;

2 个答案:

答案 0 :(得分:0)

您可以使用scandir()函数

$dir  = '/tmp';
$file = scandir($dir);
print_r($file);

答案 1 :(得分:0)

我认为这是您要查找的递归函数:

function dir_walk($dir, $q) {
    $q = trim($q);
    $exclude = array('.', '..', '.htaccess'); 
    if($dh = opendir($dir)) {
        while(($file = readdir($dh)) !== false) {
            if(in_array($file, $exclude)) { continue; }
            elseif(is_file($dir.$file)) {
                if($q === '' || strpos(strtolower($file), $q) !== false) { 
                    echo '<a href='.$dir.$file.'>'.$dir.$file.'</a><br/>';
                    }
                }
            elseif(is_dir($dir.$file)) {
                dir_walk($dir.$file.DIRECTORY_SEPARATOR, $q);
                }
            }
        closedir($dh);
        }
    }

$q = (isset($_GET['q'])) ? strtolower($_GET['q']) : ''; 
dir_walk('/data/', $q);

编辑:数据必须是主目录的绝对路径,并以目录分隔符“ / data /”结尾