need help with ecard home page - was PHP tutor in San Francisco

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
My thanks to all the people who offered to help me with homework
assignment for the PHP class I'm taking. The assignment requires the PHP
script to randomly display 16 thumbnails and when the user clicks on one
of them, to display the full-size image. Here's what I've done so far:

1) Placed the thumbs in a directory called:


2) Placed the fullsizes in a directory called:


3) Set permissions for the directories to 711 and for the images to 444

4) Written a script called ecards.php and placed it in a directory called:


(PHP scripts can *only* be executed from the php directory, or a
subdirectory thereof.)

The script can be accessed at:

As you can see, there are a few errors, and I'm not sure what's causing
them. The first one seems to be the result of my passing too large a
number of random array elements. I don't understand why this is, since
there are 139 images. Makes me think the array is not being created

As for the second error, it would appear I have a syntax error, but I'm
not sure what it is.

My thanks for any help folks here can provide, and apologies if I don't
follow posting or coding conventions here. If I've not followed proper
protocols, please let me know and I'll be sure to fix it.

Okay, here's the source code:

<a href="?dir=anna_may_wong">Anna May Wong</a><br />

    //Relative page
    //$dir = '../images/anna_may_wong';

    //Absolute path
    $image_dir = dirname(__FILE__) . '/../images/';
    $ecard_dir = 'anna_may_wong/';
    $url_dir = 'anna_may_wong';
    if(isset($_GET['dir'])) {
       $url_dir = $_GET['dir'];
       $ecard_dir = $_GET['dir'] .'/';
    $dir = $image_dir . $ecard_dir;

     // How to read the names of files in a directory.
     // FUNCTIONS:
     //    open the directory and get a handle (directory handle)
     //    read the the names of the files one at a time.
     //        this reading process doesn't distinguish between files
and directories.
     //           EVERYTHING IS A FILE, particularly directories and
regular files.
     // opendir()
     // readdir()
     // closedir()
     // array_rand()
     // is_dir()

     // Open
     // shutup operator silences warnings.
     $full_sized = "";
     if(isset($_GET['img'])) {
        $full_sized = "<img
src='/~dgoodi01/images/$url_dir/fullsize/".$_GET['img'].'\' />';
     $images = array();
     if($dh = opendir($dir)) {
        // read the files with readdir($dh);
        while($file = readdir($dh)) {
            // we don't want directories: ., .., .DS_Store, thumbs
            // our script is in /users/dputnam/public_html/php
            if(is_dir($dir.$file)) { continue; }
            if(strstr($file,'.jpg')) {
                if(file_exists($dir.'thumbs/'.$file)) {
                     $images[] = $file;

     // We have an array that contains all of the images.
     // $images has all images in it.
     // We want a random selection.
     // Fortunately, we have array_rand(ANARRAY, number)
     $num = 16;
     // array_rand returns the index numbers
     $indexes = array_rand($images, $num);


     $for_view = '';
     foreach($indexes as $idx) {
         $for_view .= "<a
src=\"/~dgoodi01/images/$url_dir/thumbs/" .$images[$idx].'" width="90"
     print $for_view;
     print $full_sized;

Re: need help with ecard home page - was PHP tutor in San Francisco

Quoted text here. Click to load it


Quoted text here. Click to load it

Why not:

  $image_dir = '../images/';

Quoted text here. Click to load it


Quoted text here. Click to load it

Explicitly compare against FALSE, since readdir() might return a
filename which is implicitly converted to boolean FALSE.

  while (($file = readdir($dh)) !== false) {

Quoted text here. Click to load it

Although not too serious an issue in your application, you should
give some careful attention to the PHP manual's security section.  
In particular, concerning filesystem security:


Filesystem Security:

Quoted text here. Click to load it

The general approach you used works for me.  Try using `print_r()'
to check the contents of $images and $indexes to see if their
contents are what you expect them to be.  It seems like your
$images array isn't being populated how you think it is.  Problems
with $images might be causing the errors in `array_rand()' and


$email = str_replace('sig.invalid', '', $from);

Site Timeline