Show images from a file with php

0

In function of a $ ID variable I have to take out one image or another. The code works perfect from 0-9, but when you get to number 10 it cascades and tells me the following:

Undefined offset: 10 in C:\xampp\htdocs\aa.php on line 7

This is my code:

<?php
session_start(); // session start
$getvalue = $_SESSION['ID']; // session get
$IMAGEN = $_SESSION['ID'];
$files = glob("IMAGENES/*.*");
if (count($files) > 0) { // make sure at least one image exists
        $img = $files["$IMAGEN"]; // first image
        echo "<img src='$img' height='150' width='150' /> ";
    } else {
       // possibly display a placeholder image?
    }
?>
    
asked by Mariopenguin 31.05.2017 в 01:36
source

1 answer

0

If there is only one image per worker, it would be something like that, so there is no need to use the glob. In $IMAGEN do you have the file extension? If not, you would have to add it both in file_exists and in the html code of img .

<?php
session_start(); // session start
$getvalue = $_SESSION['ID']; // session get
$IMAGEN = $_SESSION['ID'];

if ( file_exists("IMAGENES/$IMAGEN") ) {
   echo "<img src='IMAGENES/$IMAGEN' height='150' width='150' /> ";
} else {
   // possibly display a placeholder image?
}
?>

In case a worker had several images you would need a glob as you were using, but you would have to use within glob variable $IMAGEN .

    
answered by 31.05.2017 в 09:16