Showing the latest image in PHP

Mastah

the funny thing is \r\n i did
Oct 25, 2010
739
41
Would like the same that Josh has like when you're going to his website or you're viewing his screenshot that you have this header above with the logo and that it's showing the latest uploaded screenshot. I'm not good at programming and I've never been and that's the biggest reason why I've decided to make a thread about it but also because people got annoyed when I asked it in the shoutbox which I'm totally understanding and which in my case I would think too when there's someone constantly asking about it when you can make a thread.

I remember sindre giving one of his beta's which I've unfortunately lost and since he's rather not around no more no reason to ask him for it because he had the exact same thing Josh ( Rastalulz ) had and that's the exact thing I was searching for.

If anyone has the code be pleased to share It would be much appreciated <3
 

brsy

nah mang
May 12, 2011
1,530
272
Google is your best friend.... This solution was literally the first result on google.

pYB9dM-x.png


PHP:
<?php

    $images = glob('*.{gif,png,jpg,jpeg}', GLOB_BRACE); //formats to look for

    $num_of_files = 1; //number of images to display

    foreach($images as $image)
    {
         $num_of_files--;

         if($num_of_files > -1) //this made me laugh when I wrote it
           echo "<b>".$image."</b><br>Created on ".date('D, d M y H:i:s', filemtime($image)) ."<br><img src="."'".$image."'"."><br><br>" ; //display images
         else
           break;
    }
?>
 

RastaLulz

fight teh power
Staff member
May 3, 2010
3,935
3,936
I assume you don't want a script that gets the most recent image (or file), but a script that will display a specific file, instead of linking directly to it?
 

RastaLulz

fight teh power
Staff member
May 3, 2010
3,935
3,936
Here's how you'd do it at a very fundamental level:
PHP:
<?php

$fileDir = 'files/';

if (!isset($_GET['file']) || !file_exists($fileDir . $_GET['file']))
{
    echo 'What the fuck are you doing?';
}
else
{
    $fileName = htmlentities($_GET['file']);

    echo $fileName;
    // echo '<img src="files/' . $fileName . '">';
}

You'd then have your program URL setup like this:
Code:
http://snap.example.com/file.php?file=file-name.png

You can also add an .htaccess file to pretty the URL if you want.
 

Users who are viewing this thread

Top