tags:

views:

23

answers:

2

I am trying to print a UL list of images ending with _th.jpg returned from a specific folder. All i get back is array()

<?php
require '../../config.php';
$conn = new PDO(DB_DSN, DB_USER, DB_PASS);

$id = (int)$_GET['id'];
$q = $conn->query("SELECT * FROM cms_page WHERE id=$id"); 
$project = $q->fetch(PDO::FETCH_ASSOC);
$q->closeCursor();

$imagesDir = 'public/images/portfolio/'.$project['slug'].'/';
$images = glob($imagesDir . '*_th.{jpg,jpeg,png,gif}', GLOB_BRACE);

echo $imagesDir ;
print_r($images);

?>

<h1><?=htmlspecialchars($project['title'])?></h1>
<?php 
if ($images < 1) { 
    echo '<div id="slider">';
    echo '<ul>';
     foreach($images as $key => $value) { 
      echo '<li><a href="public/images/portfolio/i-vis/1.jpg" rel="shadowbox[iVis]" title="Fully Content Managed Website"><img src="public/images/portfolio/'.$project['title'].'/'.$value.'.jpg" width="160" height="96" /></a></li>';
     }
    echo '</ul>';
    echo '</div>';
} else { 
    echo 'There are currently no images to display for tihis project.';
}
?>

Anyone got any ideas?

A: 

Hi,

First of all : are you sure you are reading from the right directory ?

What I mean, is : does 'public/images/portfolio/'.$project['slug'].'/' really exist, and is accessible from your script, using this relative path, in this PHP script ?

(You can test that with is_dir, for instance)


As a precaution, when working with files and directories, I like using absolute paths, so I always know to which directory exactly I'm accessing.

For instance, something like this :

$imagesDir = '/var/www/public/images/portfolio/'.$project['slug'].'/';

If you do not know the absolute path to your directories, you can use dirname(__FILE__), to get the absolue path to the current PHP file -- and then, starting from that file, use a relative path ; for instance :

$imagesDir = dirname(__FILE__) . '/public/images/portfolio/'.$project['slug'].'/';

This way, you don't have to care about the current working directory : your path will always be the same, as it's not relative.


Hope this helps...

If it doesn't : first of all, make sure you are accessing the right directory...

Pascal MARTIN
Thanks pascal. I used your dirname to find the absolute path. Then added this to the script and im now printing an array of images.
Andy
Perfect ;-) Have fun !
Pascal MARTIN
Actually pascal the trouble is with this i want to use the path to create the image and now its using the server path...any ideas?
Andy
Oh :-( You'll probably have to use a "systme path", when accessing the files on the server (ie, dirname, for the path used with glob) ;; and another "web path" for the HTML you are outputting
Pascal MARTIN
A: 
<h1><?=htmlspecialchars($project['title'])?></h1>

Is <? a valid PHP opening tag?

Since your last block of PHP code isn't apparently executed (since it would either print an empty ul or the text from the else block) I suspect the PHP doesn't get parsed correctly.

xtofl
it using PDO i believe
Andy
the Short Opening Tag `<?` is only allowed when `short_open_tag` is 1. You could check this by printing something after the `<h1>` block.
xtofl