PHP Gallery does not load large images. Problem with while

0

I have been working around a gallery for hours and I can not get it to work, I am a PHP beginner and I would appreciate any help. Attachment code below.

In the gallery there are thumbnails and when you click the big picture takes you. The problem is that the big images do not open. The gallery is on the page of each product, where each one has an ID and a folder of its own, that is, the product 200 has the folder 200 where there are the large photos together with a subfolder 'thumbs'.

Product folder - > large images.

Thumbs folder - > small images.

To show the 2 folders from PHP, I understand that logically you have to open 2 directories with opendir (product folder and thumbs folder), therefore you have to do 2 whiles through the images of each directory, but I'm not sure if the code is fine, they are put in a row, is that correct?

As I said the thumbnails in the thumbs folder, they are loaded, but those in the directory of the large photos, no. This is the code of the gallery - On the other hand, if I put an image on the route.jpg as such instead of the variable $ file2 then the large image is loaded.

I have checked the routes are fine, I sense that it must be a logic failure or question of keys ... I hope to have explained. Thanks in advance.

Product code.php (inside a div):

<?php
$ruta = "img/content/producto/$_GET[id]/thumbs/"; // Indica ruta para thumbnails
$filehandle = opendir($ruta); // Abrir archivos
while ($file = readdir($filehandle)) {
    if ($file != "." && $file != "..") {
        $tamanyo = getimagesize($ruta . $file);

        $ruta2 = "img/content/producto/$_GET[id]/"; // Indica ruta para imagen grande
        $filehandle2 = opendir($ruta2); // Abrir archivos
        while ($file2 = readdir($filehandle2)) {
            if ($file2 != "." && $file2 != "..") {
                $tamanyo2 = getimagesize($ruta2 . $file2);
            }
        }
?>

To show the gallery:

     <a data-fancybox="gallery" href="img/content/producto/<?php echo $_GET['id']?>/imagen.jpg">
     <img src="<?php echo $ruta.$file ?>" width="200px" height="auto" style="float:left;"></a>

To close

    <?php
    }
}
closedir($filehandle); // Fin lectura archivos
closedir($filehandle2); // Fin lectura archivos
    
asked by Mamen Maria 27.09.2017 в 22:36
source

3 answers

0

I do not know if you've already tried the following, but in trying to concatenate the route

<a data-fancybox="gallery" href="<?php echo $ruta2.$file2?>">
    
answered by 27.09.2017 в 23:09
0

If I understood correctly. You want to present in a page a gallery of small images that are in a folder and give you as a link to those images your corresponding in another folder.

I will say what I have done to reproduce your problem.

  • I have assumed that there is a folder called uploads and within it there are several folders with a numeric id
  • For this test our folder will be uploads/200 , that is, I have assumed that our current id is 200. That you then change it with the GET .
  • Within uploads/200 there is another folder called thumb where the small images are located.

Understanding that, instead of manipulating the files, we will use glob to determine what is in the folders .

We assume that both in the folder 200 with in their respective folder thumb there are only images . If not, then we must refine the search we do with glob to get only the images.

Everything else is commented on in the code.

I've tried it in a real scenario and it works. Create small images and when I click on any of them it takes me to the big image.

<?php

/*----Cambiar esta parte por tus datos reales----*/

$id=200;  //Cambiarlo por el _$GET
$urlThumb = "uploads/$id/thumb/";
$urlGrande = "uploads/$id/";
//URL del tipo http://wwww.dominio.com  hasta antes de la carpeta de imágenes
$urlReal="http://www./";

/*---------------------------------------------*/


/*Creamos un array con todas las imágenes de ambos directorios*/
$arrThumb = glob($urlThumb.'*.*');
$arrGrande = glob($urlGrande.'*.*');

//print_r($arrThumb);
//print_r($arrGrande);

/*Variable para concatenar nuestro contenido HTML*/
$strHTMLThumb="";
foreach ($arrThumb as $k=>$v)
{
    /*src para el thumb y para las imágenes grandes*/
    $srcThumb=$urlReal.$v;
    /*quitamos la palabra thumb del directorio así obtenemos el directrio de las imagens grandes*/
    $srcGrande = str_replace('thumb/', '', $v);

    $strHTMLThumb.='<a data-fancybox="gallery" href="'.$srcGrande.'">
     <img src="'.$srcThumb.'" width="200px" height="auto" style="float:left;"></a>';
}

/*Imprimimos el resultado concatenado*/
echo $strHTMLThumb;

?>
    
answered by 28.09.2017 в 00:49
0

Try this way, he should go within the second while

<?php
$ruta = "img/content/producto/$_GET[id]/thumbs/"; // Indica ruta para thumbnails
$filehandle = opendir($ruta); // Abrir archivos
while ($file = readdir($filehandle)) {
    if ($file != "." && $file != "..") {
        $tamanyo = getimagesize($ruta . $file);

        $ruta2 = "img/content/producto/$_GET[id]/"; // Indica ruta para imagen grande
        $filehandle2 = opendir($ruta2); // Abrir archivos
        while ($file2 = readdir($filehandle2)) {
            if ($file2 != "." && $file2 != "..") {
                $tamanyo2 = getimagesize($ruta2 . $file2);
                ?>
                <a data-fancybox="gallery" href="<?php $ruta2.$file2 ?>">
                <img src="<?php echo $ruta.$file ?>" width="200px" height="auto" style="float:left;"></a>

     <?php
           }
        }

    }
}
closedir($filehandle); // Fin lectura archivos
closedir($filehandle2); // Fin lectura archivos
    
answered by 28.09.2017 в 00:49