Jump to content

Display A Picture


AV1611

Recommended Posts

I have no code snippet because I've never done this and have no idea how or what to read...

simple:

in the current directory, there are three jpg files, xxx.jpg yyy.jpg zzz.jpg.

there may be more or less, I can do pagination no problem there.

I simply need it to:
a. scan the current directory for .jpg images
b. create hyperlinks to them

easy, but I don't know how to scan the directory so I can create the links...

thanks
Link to comment
https://forums.phpfreaks.com/topic/13192-display-a-picture/
Share on other sites

It's done with opendir/readdir/closedir, eg:

[code]<?php
$strDir = "/my/images";

if($dHandle = opendir($strDir))
{
while (false !== ($strFile = readdir($dHandle)))
{
    if($strFile != "." && $strFile != ".." && !is_dir($strFile))
    {
        $arrayExt = explode(".", $strFile);
        $strExt = $arrayExt[count($arrayExt)-1];

        if($strExt == "jpg" || $strExt == "jpeg")
            echo $strDir . "/" . $strFile;
    }
}
    closedir($dHandle);
}
?> [/code]

HTH, Zac.
Link to comment
https://forums.phpfreaks.com/topic/13192-display-a-picture/#findComment-50770
Share on other sites

So do I simply modify this one line if I want to include bmp png gif also?

if($strExt == "jpg" || $strExt == "jpeg" || $strExt == "Whatever")

[!--quoteo(post=389201:date=Jun 29 2006, 07:11 AM:name=heckenschutze)--][div class=\'quotetop\']QUOTE(heckenschutze @ Jun 29 2006, 07:11 AM) [snapback]389201[/snapback][/div][div class=\'quotemain\'][!--quotec--]
It's done with opendir/readdir/closedir, eg:

[code]<?php
$strDir = "/my/images";

if($dHandle = opendir($strDir))
{
while (false !== ($strFile = readdir($dHandle)))
{
    if($strFile != "." && $strFile != ".." && !is_dir($strFile))
    {
        $arrayExt = explode(".", $strFile);
        $strExt = $arrayExt[count($arrayExt)-1];

        if($strExt == "jpg" || $strExt == "jpeg")
            echo $strDir . "/" . $strFile;
    }
}
    closedir($dHandle);
}
?> [/code]

HTH, Zac.
[/quote]
Link to comment
https://forums.phpfreaks.com/topic/13192-display-a-picture/#findComment-50779
Share on other sites

Script works great, but one more question:

What about caps? If the file is xxxx.jpG
is there a way to fix those, or do I add those permutations to $strExt clause?

// EDIT Never mind, I did it like this:

<?php
$strDir = "./corp/";
if($dHandle = opendir($strDir))
{
while (false !== ($strFile = readdir($dHandle)))
{
if($strFile != "." && $strFile != ".." && !is_dir($strFile))
{
$arrayExt = explode(".", $strFile);
$strExt = strtolower($arrayExt[count($arrayExt)-1]);

if($strExt == "jpg" || $strExt == "gif" || $strExt == "bmp")
//echo $strDir . "/" . $strFile;
echo "<a href='". $strDir.$strFile ."'>".$strFile."</a><br/>";
}
}
closedir($dHandle);
}
?>
Link to comment
https://forums.phpfreaks.com/topic/13192-display-a-picture/#findComment-50788
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.