Jump to content

[SOLVED] Displaying images from a directory...


jderosa3

Recommended Posts

First off I am using exec-php with Wordpress to execute PHP code in my "static" pages - I assume what I want to do it simple, but I am having issues.

 

What I want to do is just display all the files (JPGs) from a specified directory. I coded the following:

 

<?php
$dir = "./flash/portfolio/branding/images/";
//open dir
if ($opendir = opendir($dir))
{
   //read dir
while (($file = readdir($opendir)) !== FALSE)
{
if ($file!="."&&$file!="..")
echo $file."<img src='$dir/$file'><br />";
   }
}
?>

 

but... all this does is list the files names and right next to them it displays a broken icon as if the graphics are not there... what gives... can any one test this... and let me know their results... or is there another method to display graphics in this manner.

 

Thanks!

Link to comment
Share on other sites

Try this:

<?php
$dir = "./flash/portfolio/branding/images/";
//open dir
if ($opendir = opendir($dir))
{
   //read dir
while (($file = readdir($opendir)) !== FALSE)
{
if ($file!="."&&$file!="..")
echo $file."<img src='$dir/$file'><br />";
   }
}
?>

Link to comment
Share on other sites

If all you are looking for is a file name. Try glob.

 

$jpgs = glob($dir . "*.JPG");

foreach ($jpgs as $jpg) {
    echo "<img src='{$jpg}' /><br />";
}

 

A bit less code and easier, in my opinion.

 

As far as it showing an x, the path needs to be relevant to the actual web path not the server path. So make sure that is what it is doing.

Link to comment
Share on other sites

Try this:

<?php
$dir = "./flash/portfolio/branding/images/";
//open dir
if ($opendir = opendir($dir))
{
   //read dir
while (($file = readdir($opendir)) !== FALSE)
{
if ($file!="."&&$file!="..")
echo $file."<img src='$dir/$file'><br />";
   }
}
?>

 

My bad... that was how my code was... it must have converted the && to && when i pasted it in... it still gives me those broken image icons... my question is... how can it list the correct files names of the directory and not the image itself... it must be reading the directory correctly if it can list the files, correct?

Link to comment
Share on other sites

If all you are looking for is a file name. Try glob.

 

$jpgs = glob($dir . "*.JPG");

foreach ($jpgs as $jpg) {
    echo "<img src='{$jpg}' /><br />";
}

 

A bit less code and easier, in my opinion.

 

As far as it showing an x, the path needs to be relevant to the actual web path not the server path. So make sure that is what it is doing.

 

So would i in essence put this in my page like this:

 

<?php
$jpgs = glob($dir ./flash/portfolio/branding/images/ "*.JPG");

foreach ($jpgs as $jpg) {
    echo "<img src='{$jpg}' /><br />";
}
?>

 

Im sorry I have been so rusty with this stuff...

Link to comment
Share on other sites

mmm no.

 

$dir = "./flash/portfolio/branding/images/";
$jpgs = glob($dir . "*.JPG");

foreach ($jpgs as $jpg) {
    echo "<img src='{$jpg}' /><br />";
}

 

But that will only pull JPG that end in all caps. So if the .jpg is all caps or all lowercase modify accordingly.

Link to comment
Share on other sites

OK... I used as you said...

 

<?php
$dir = "./flash/portfolio/branding/images/";
$jpgs = glob($dir . "*.jpg");

foreach ($jpgs as $jpg) {
    echo "<img src='{$jpg}' /><br />";
}
?>

 

I know that is grabbing the correct amount of files.. in my case... 5... but they all have broken image icons.. so obviously this is working correctly... but it must be something in wordpress causing this... I just can't understand... especially because it is grabbing the correct number of files... I will look further into this...

 

thanks for the help so far... I truly appreciate it...

 

 

Link to comment
Share on other sites

I am not sure what is going on... I am assuming it is wordpress... try doing a sample installation and running the exec-php plugin...

 

When I check the properties on the broken image icons... I get the following code:

<img src="./flash/portfolio/branding/images/philco-sticker.jpg">

 

the flash directory is in the root of my site... for example:

 

http://www.xyz.com/flash/portfolio/branding/images/philco-sticker.jpg

 

I would rule out that I am doing something wrong if someone else was able to try this and produce the same results as me... driving me nuts...

Link to comment
Share on other sites

What happens if you add the http://www.xyz.com onto the path?

 

    echo "<img src='http://www.xyz.com{$jpg}' /><br />";

 

If that does not work try this to remove the initial dot:

    $jpg = substr($jpg, 1);
    echo "<img src='http://www.xyz.com{$jpg}' /><br />";

 

And see if that makes the image display.

 

Link to comment
Share on other sites

Why do you have the initial . ??? Try removing that and see what happens.

 

@xtopolis

glob carries the directory path with it, if it was specified in the search terms.

 

I tried it and that doesn't work... nothing shows up at all.. I believe I need that "." because that throws me back to the root of the site.

 

Wordpress works within side of it's wp-content directory... so in order for me to access something in the root of my site... I assume I need that beginning "."

Link to comment
Share on other sites

;D ;D ;D ;D

 

This did it!!!!

<?php
$dir = "./flash/portfolio/branding/images/";
$jpgs = glob($dir . "*.jpg");

foreach ($jpgs as $jpg) {
    $jpg = substr($jpg, 1);
    echo "<img src='http://www.xyz.com{$jpg}' /><br />";
}
?>

 

why this worked this way is beyond me! Thank you very very much!!!!

 

No wonder this is the PHP freaks forum... you guys (and gals) rock!

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.