Retrieve scandir images from PHP and print incrementally according to the number of occurrences

0

I need to retrieve images from a folder and only have the reference number written to the database. Also I have to create an increment, because for each reference can have n images.

By combining the scandir and strstr functions of PHP I can recover, but despite correctly printing what I need, in example 1 , it does not filter the other results, making it difficult to create the correct increment, such as photo-1- ... photo-2- ... etc.

By adding the line break you notice the other hidden results, making the increment catch all results of the folder. Thanks for any help.

Images are saved like this: Ex: photo-1-0023.jpg, photo-2-0023.jpg

$dir    = '../assets/Imagens/';
$files = scandir($dir);


foreach ($files as $key => $value) :
    $conta=1;
    //$domain = strstr($value, "foto-".$conta++."-".$row['referencia']);
    // Exemplo 1

    $domain = strstr($value,  $row['referencia']);

    echo $domain ; 
    // imprime 00234.jpg00234.jpg00234.jpg00234.jpg00234.jpg00234.jpg (6 imagens)

    echo $domain.'<br>' ; 
    //imprime 00234.jpg


    //00234.jpg
    //00234.jpg



    //00234.jpg
    //00234.jpg
    //00234.jpg

endforeach;
    
asked by anonymous 11.04.2016 / 16:20

2 answers

-1

Thanks for the suggestion, Daniel, but I ended up resolving otherwise. Not the most elegant but it worked both for printing and for changing the files.

<?php
    //select para obter a referencia

    $d=0;
    $m=0;                    

    //loop para buscar as imagens foto-1, foto-2, ...
    for ($i=0; $i<99; $i++) :
        $fotopasta ="assets/Imagens/foto-".++$m."-" .$row['referencia'] .".jpg";

        //se a imagem existir insere no array
        if(file_exists($fotopasta)) :
            $fotopastaArray[] = $fotopasta;
        endif;
    endfor;

    //percorro a string e descubro o numero da ultima imagem
    $qtd_fotos = substr(end($fotopastaArray), 20,2);

    //monto outro loop com o numero correto de ocorrencia
    for ($n=0; $n < $qtd_fotos; $n++) :
        $foto ="assets/Imagens/foto-".++$d."-".$row['referencia'].".jpg"; ?>
        <?php if (file_exists($foto)) :  ?>
            <li><img src="<?php echo $foto ; ?>" alt="<?php echo utf8_encode($row['nome']) ; ?>"></li>
        <?php else : endif;  
    endfor; 
?>
    
19.04.2016 / 14:19
1

You can make it easier by saving the partial path in the bank and avoiding a folder loop that is more costly to execute, saves "/assets/Images/image001.jpg" in the database and retrieves it with a:

"http: //". $ _SERVER ['SERVER_NAME']. $ _SERVER ['REQUEST_URI']. $ row ['img_url'] "

$ row being the array that will come from the loop in the bank.

If it's impractical, I can help you think along the same lines you're currently doing.

    
12.04.2016 / 15:53