Jump to content


Photo

Display A Picture


  • Please log in to reply
4 replies to this topic

#1 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 29 June 2006 - 10:58 AM

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

#2 heckenschutze

heckenschutze
  • Members
  • PipPipPip
  • Advanced Member
  • 257 posts
  • LocationAustralia

Posted 29 June 2006 - 11:11 AM

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

<?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);
}
?>

HTH, Zac.

#3 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 29 June 2006 - 11:45 AM

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) View Post[/div][div class=\'quotemain\'][!--quotec--]
It's done with opendir/readdir/closedir, eg:

<?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);
}
?>

HTH, Zac.
[/quote]


#4 heckenschutze

heckenschutze
  • Members
  • PipPipPip
  • Advanced Member
  • 257 posts
  • LocationAustralia

Posted 29 June 2006 - 11:56 AM

Yes. Note: I haven't tested it, as I wrote it on the spot.

#5 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 29 June 2006 - 12:19 PM

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);
}
?>




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users