Show only the current user profile image with PHP

0

Good morning.

Someone will know by chance how I can show only the current profile image with PHP

Currently I have this like this:

$path = "files/".$id;
if(file_exists($path)){
    $directorio = opendir($path);
    while ($archivo = readdir($directorio)){
        if (!is_dir($archivo)){
            echo "<div data='".$path."/".$archivo."'><a href='".$path."/".$archivo."' title='Ver Imagen'><span class='glyphicon glyphicon-picture'></span></a>";
            echo "$archivo <a href='#' class='delete' title='Eliminar' ><span class='glyphicon glyphicon-trash' aria-hidden='true'></span></a></div>";
            echo "<img src='files/$id/$archivo' width='300' />";
        }
    }
}

But in doing this, all the profile pictures that the current user has are shown, and I just want you to show me the last one that came up.

I made these changes but it's not there yet:

$path = "files/".$id;
if(file_exists($path)){
    chmod($path, 0777);
    unlink($path);
    $directorio = opendir($path);
    while ($archivo = readdir($directorio)){
        if (!is_dir($archivo)){
            echo "<img src='files/$id/$archivo' width='200' height='200' 'image-align:center'/>";
        }
    }
}
    
asked by Noctis 04.09.2018 в 23:12
source

1 answer

1

Friend, I did the test and your original code works if you put the variable $id with a value. Maybe you are not sending the correct value or the directories are not created.

I guess that within the files folder, you have a folder with every id and inside the profile photo. That is the scheme that works in this logic and if that is the only picture presents it without problems. In case you want to upload more photos of that user in the same folder, you can create a subdirectorio or put all the profile photos with the same name, such as thumbnail.png and validate the name.

  

EDITING : to not validate by name, I modified the function so that   orders the modified images and only shows the last one.

<?php
    $id = 1;
    $path = "files/".$id;
    if(file_exists($path)){
        $directorio = opendir($path);
        while ($archivo = readdir($directorio)){
            if (!is_dir($archivo)):
                $archivos[filemtime($path.'/'.$archivo)] = $path.'/'.$archivo;
            endif;
        }
        closedir($directorio);

        //Ordenar el array
        ksort($archivos);

        //Buscar la última modificacida
        $ultimaModificada = end($archivos);

        //Compara todas las URLs y solo muestra la última modificada
        foreach ($archivos as $archivo){
            if ($archivo == $ultimaModificada){?>
                <div data="<?php echo $path; ?>-<?php echo $archivo; ?>">
                    <a href="<?php echo $archivo; ?>" title="Ver Imagen">
                        <span class='glyphicon glyphicon-picture'></span>
                    </a>
                    <a href="#"" class="delete" title="Eliminar">
                        <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
                    </a>
                </div>
                <img src="<?php echo $archivo; ?>" width="300" />
            <?php
            }
        }
    }

Greetings!

    
answered by 05.09.2018 / 03:00
source