Search file via file_exists with partial name

Asked

Viewed 80 times

1

I need to verify the existence of a file in two domains.

However, the file format that is saved in the database does not match the one saved on the server, due to a lag of a few seconds (the file name format is shown in the example below).

File that exists on the server https://www.dominio01.com.br/sistema/modulos/consultas/consulta_87314134987_02102017135619.pdf

File you are bringing https://www.dominio01.com.br/sistema/modulos/consultas/consulta_87314134987_02102017135613.pdf

As you can see, there is a difference in the last 2 characters (which represent the seconds... probably in the generation routine of this PDF, there is a delay of a few seconds that differs from the date recorded in the database).

$dir01 = "https://dominio01.com.br/sistema/modulos/consulta/consultas/";
$dir02 = "https://dominio02.com.br/sistema/modulos/consulta/consultas/";

$documento = preg_replace("/[^0-9]/", "", $item['retCNPJCPF']);
$dataDoc = new DateTime($item['retDataHora']);

$filename = "consulta_".$documento."_".$dataDoc->format('dmYHis').".pdf";

if(file_exists($dir01.$filename)){
    $lnkConsultas = "Consulta disponível no dominio 01";
}
elseif(file_exists($dir02.$filename)){
    $lnkConsultas = "Consulta disponível no domínio 02";
}

I wonder if it is possible to bring the files without the need to inform the seconds, and bring the occurrences of this... changing the filename, and searching through some regular expression. But I have no idea how to do it.

PS: I cannot use "glob". This will return blank results, because the files are in other domains.

  • I hadn’t read the "I can’t use it glob.". :(

  • @Inkeliz unfortunately tried with glob, but the array returns blank :/

1 answer

0

Unfortunately, you will not be able to get the file from another regex server. The only easy and practical way to solve this problem is by using glob, but the directory listing should be active. If the webserver is yours, simply activate it.
(For more details of a look in this publication (en-US))

Other than that, you’ll only get the possible links if:

Using brute force

Based on your speech: "there is a delay of a few seconds", I created a function that will search for the file depending on a offset, or a number that you will inform by the file, for example, forcaBruta("...13", 6) (13 seconds), will search for:

...13 (atual) - não encontrado
...14 (+1)    - não encontrado
...15 (+2)    - não encontrado
...16 (+3)    - não encontrado
...17 (+4)    - não encontrado
...18 (+5)    - não encontrado
...19 (+6)    - arquivo encontrado

Logically, the offset will not test for smaller numbers as the server will receive the file after you (if this is not the case, simply arrange the function to "-1 second"):

<?php
    use \DateTime as DT;
    function forcaBruta($consulta, $offset = 0){
        preg_match("/_(\d+?)\.pdf$/", $consulta, $arr); // Obtem os ultimos números do arquivo
        $dataDoc = DT::createFromFormat("dmYHis", $arr[sizeof($arr)-1]); // Cria um DateTime com o número recebido para não haver erros na força bruta (ex.: 61 segundos)
        for ($x=0; $x<=$offset; $x++) {
            $url = preg_replace("/_\d+.pdf$/", "_".$dataDoc->format("dmYHis").".pdf", $consulta);   // Obtem o novo URL
            if (@fopen($file, "r")) // Se possível, substitua para file_exists(), no meu caso, não estava funcionando...
                return $url; // Retorna o URL novo se ele existir
            $dataDoc->modify("+1 second"); // Adiciona 1 segundo ao doc da data ("-1 second" pra fazer o inverso)
        }
        return false;
    }
?>

This way, when using:

forcaBruta("https://www.dominio01.com.br/sistema/modulos/consulta/consultas/consulta_87314134987_02102017135613.pdf", 6)

the function will return the url of the existing file of 02102017135613 until 02102017135619, if it does not exist, then it will return false.


Demonstration:

Demonstração

Browser other questions tagged

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