Search for file in directory

Asked

Viewed 2,564 times

4

I have a directory on the network with thousands of TXT files generated by another software hired in the company.

The file is named with the following logic:

 (cpf do cliente)(data da insercao)(hora insercao).txt
 1234567894120150501142024.txt

I need to select the txt corresponding to the CPF of the informed client, however I am not able to search the file.

  • Do you have the necessary information to find this file? (Cpf ,date and time of insertion)

  • I only have Cpf, and this file is unique, generated only when inserting a new client. Wanted something like LIKE mysql, so that it would search all files with Cpf at the beginning.

  • published my reply

2 answers

5


Uses the function glob() for that reason.

foreach (glob("00000000000*") as $file) {
    $user = $file;
}

Note that in the default definition there is a * which serves to define that everything that comes after the $cpf is irrelevant to the search, so you will receive as return all the files that have at the beginning a value that is equal to the CPF equal to the searched.

2

I didn’t test it, but I believe glob() will solve the problem. If you know the CPF, you can do something like

$cpf = "12345678900*";
    foreach (glob($cpf) as $arq) {
    echo "$arq" ."\n";
}

it returns the name of the file that has Cpf that you passed.

Browser other questions tagged

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