How to browse a folder without knowing the name in PHP

Asked

Viewed 63 times

1

Hello, I need your help, I am making a very complex system (for a change) that in one of the parts of this system, has to enter a folder, however, this folder will have an undefined name, the system will have to find what was not defined, in simplification: My system needs to go through in a folder, even not knowing its name, since most of the functions I use, need the folder name, well and for those who find it easy this, know that in this system, it will only use functions that do not connect to a database. Now I will give you examples of this:

fopen("vaidarerrado/(PASTA NÃO DEFINIDA)/index.php","r");

If you observe, the function "fopen" will go through the folder "vanity" all right, until when it arrives at something undefined that in this case I marked as: "(FOLDER NOT DEFINED)", which in this case obviously gives a huge error, because as a function can go through a folder, that not even the programmer informed?, (alias the system wants to pass the "(FOLDER NOT DEFINED)" because it aims to access the file "index.php" even without knowing which folder it is in) ,in another simplification: go through a folder without knowing the name to find a specific file, and the only function I know, that takes SOME information from an undetermined folder, and the function:

glob();

EXAMPLE:

count(glob("vaidarerrado/{*}",GLOB_BRACE));

the code "glob" above, even without knowing the name of the folder, can "know of its existence" and give the number of things that have in the directory "vain", of course, this was just an example, what I want, and go through a folder without knowing its name, to find a file, or if you can, enter the name of the folder. I hope you help me or get to the "Index.php" GOING THROUGH THE UNDEFINED FOLDER or somehow, getting the name of the folder, good luck to you, I expect some answer ;p (kk)

  • Why don’t you use the function glob if she does what you need?

  • the answer is in the question itself... normally the glob() or equivalent is used.

1 answer

4


Consider the following file structure:

 pasta/
     d46as54d9as/
         index.php

With the function glob, just reading the basics of the documentation, we can do:

foreach (glob("pasta/*/index.php") as $file)
{
    echo $file;
}

The return will be:

pasta/d46as54d9as/index.php
  • Thanks for your reply, however, even before you do it, I realized that the Count() function was for arrays, so I just realized that the glob() also returned an arrays of something, but I thought it was just numbers, so I decided to see what was in these arrays, I was surprised to find what I wanted, practically... my question was already the answer itself, but I appreciate your effort! (or even effort, who knows...)

  • 1

    That’s why I said before: just study a little more.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.