iiCe89's avatar

Understanding File Types .Jpg, .PNG .doc etc

i can upload files to my website and display the file name & view file on new Tab but how would i go about displaying what file type it is ?

For example when a file is uploaded i want it to show a PNG icon when its png file and for a doc file it shows a Doc icon and so on so the user can tell what file is what quickly

0 likes
9 replies
SaeedPrez's avatar
// Gives you an array with info, dir, filename, extension, etc..
$info = pathinfo('path/to/file.png');

// Just get extension
$ext = pathinfo('path/to/file.png', PATHINFO_EXTENSION);

Also, I believe you can get the extension via the Storage facade, but it will basically run the above code.

SaeedPrez's avatar

@iiCe89 what part of what I wrote are you unsure about? Did you try the code I gave you?

SaeedPrez's avatar

@iiCe89 you can have it in your controller.. when you fetch a model and maybe it has a related file/image, you can use that code and send the result to your view.

iiCe89's avatar

@SaeedPrez is there anyway you could demostrate using code ? apologies trying to wrap my hand around laravel so everythings a big learning curve

SaeedPrez's avatar
Level 50

@iiCe89

$directory = '/'; // Refers to /storage/app folder

foreach (Storage::files($directory) as $file) {
    // $file holds the filename

    // Get the extension
    $extension = pathinfo($file, PATHINFO_EXTENSION);

    // When the file was last modified, in Unix timestamp
    $lastModified = Storage::lastModified($file);

    // Turn the timestamp into carbon instance
    $lastModifiedCarbon = Carbon::createFromTimestamp($lastModified);
}
1 like
iiCe89's avatar

thank you ! sorry for the bother :)

Please or to participate in this conversation.