0

im working on a school project using laravel, i have a directory of images,those images will change with time, i'm trying to just display the name of those images as links without extensions but i don't know how to hide extensions, and when i click on link it dosen't work.

  <?php
$dir_nom = '../images'; // dossier listé (pour lister le répertoir courant : $dir_nom = '.'  --> ('point')
$dir = opendir($dir_nom) or die('Erreur de listage : le répertoire nexiste pas'); // on ouvre le contenu du dossier courant
$fichier= array(); // on déclare le tableau contenant le nom des fichiers
$dossier= array(); // on déclare le tableau contenant le nom des dossiers

while($element = readdir($dir)) {
    if($element != '.' && $element != '..') {
        if (!is_dir($dir_nom.'/'.$element)) {$fichier[] = $element;}
        else {$dossier[] = $element;}
    }

}

if(!empty($fichier)){

    echo "<p>images : \n\n</p>";
    echo "\t\t<ul class=rien>\n";
        foreach($fichier as $lien) {

         echo "\t\t\t<a href=\"../images$lien \">$lien</a><br>\n";  
          }
    echo "\t\t</ul>";


 }
 closedir($dir);
?>
     ?>
  • Since it's for a school project, I guess there should be teachers around to help you? – Taacoo Mar 21 '18 at 13:44
  • Not sure what you’re asking here, the code looks okay. Do you maybe need `/` after `images`? Second one, see https://stackoverflow.com/questions/2395882/how-to-remove-extension-from-string-only-real-extension – Zoe Edwards Mar 21 '18 at 13:45

3 Answers3

1

Create a controller and a route to get your images. The route parameter will be the image name so you can get it from the controller.

Route::get('img/{img}', 'ImageController@getImage')->name('image');

And your controller getImage() method will look like this:

use Intervention\Image\Facades\Image;

//...

public function getImage($img)
{
    return Image::make(asset('img/'.$img.'.jpg'))->response();
}

The asset() helpers creates an absolute path to the given resource located in the public folder.

So now, thanks to the route, the extension is not displayed anymore. If you want your image to be only accessed via this route, use the storage instead.

Finally, you can get the URL to your route using the route() helper in your view.

<a href="{{ route('image', 'imageNameWithoutExtension') }}">Link</a>
SystemGlitch
  • 2,150
  • 12
  • 27
0

You have to use base_path function to be sure that you have the correct path for open this directory. In many case, it due to wrong path so just verify the path. Or you can put the image directory in the public diectory and then you can easily have access to it. and for the path it will not be ..\imagesor ../images but just ìmages even if you are in linux distro.

Steve Ruben
  • 1,336
  • 11
  • 20
0

it worked with this :

<?php 
$dirname = 'images'; 
$dir = opendir($dirname); 
while($file = readdir($dir)) { 
  if($file != '.' && $file != '..' && !is_dir($dirname.$file)) { 
    echo '<a href="'.$dirname.'/'.$file.'">'.$file.'</a>'; } } closedir($dir); 
    ?>