Find file via file_exists with partial name

1

I need to check for a file in two domains.

However, the format of the file that is written to the database does not match the one recorded on the server, for a few seconds delay (the format of the file name is shown in the example below).

File that exists on the server link

Uploading file link

As you can see, there is a difference in the last 2 characters (representing 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 bank).

$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 would like to know 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 search through some regular expression. But I have no idea how to do this.

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

    
asked by anonymous 13.10.2017 / 16:04

1 answer

0

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

Otherwise, you'll only be able to 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 look for the file depending on an offset , or a number that you will inform to search for 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

Of course, the offset will not test for smaller numbers because the server will receive the file after you (if this is not the case, simply set 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;
    }
?>

In 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 from 02102017135613 to 02102017135619 , if it does not exist, then it returns false .

Demonstration:

    
15.10.2017 / 17:03