Jump to content

A complete newby asks a simple? question...


xymbo

Recommended Posts

Hi, I am very new to php and am playing with a little bit of code to display some thumbnail images on an html page.

 

I am confused because I can get the code to work if its placed in the image directory and called from there, but not if I try to reference the code from the root directory. The problem lies in the first 'IF' statement of the function called 'displayphotos' - line 3.

 

the code that works reads (the code that doesn't is below that:

 

<?php

 

$columns    = 4;

$thmb_width  = 118;

$thmb_height = 118

 

function getNormalImage($file){

$base = substr($file,0,strrpos($file,'_th.jpg'));

if (file_exists($base.'.jpg')) return $base.'.jpg';

elseif (file_exists($base.'.jpeg')) return $base.'.jpeg';

else return "";

}

 

function displayPhotos(){

global $columns;

$act = 0;

if ($handle = opendir(".")) {                // THIS IS THE OFFENDING VALUE

// Read all file from the actual directory

while ($file = readdir($handle))  {

// Check whether the actual item is a valid file

if (is_file($file)){

// Check whether the actual image is a thumbnail

      if (strpos($file,'_th.jpg')){

++$act;

if ($act > $columns) {

echo '</tr><tr><td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/></a></td>';

$act = 1;

} else {

echo '<td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/></a></td>';

}

     

      }

      }

}

}

}

 

?>

 

and the code that dosen't work reads:

 

<?php

 

define(GALLERY_ROOT, "./portfolio/2ded/");

$columns    = 4;

$thmb_width  = 118;

$thmb_height = 118;

 

 

function getNormalImage($file){

$base = substr($file,0,strrpos($file,'_th.jpg'));

if (file_exists($base.'.jpg')) return $base.'.jpg';

elseif (file_exists($base.'.jpeg')) return $base.'.jpeg';

else return "";

}

 

function displayPhotos(){

global $columns;

 

$act = 0;

// Open the actual directory

if ($handle = opendir(GALLERY_ROOT)) {

// Read all file from the actual directory

while ($file = readdir($handle))  {

// Check whether the actual item is a valid file

if (is_file($file)){

// Check whether the actual image is a thumbnail

      if (strpos($file,'_th.jpg')){

++$act;

if ($act > $columns) {

echo '</tr><tr><td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/></a></td>';

$act = 1;

} else {

echo '<td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/></a></td>';

}

     

      }

      }

}

}

}

 

?>

 

Any help would be great! Thanks in anticipation.

 

xymbo

Link to comment
Share on other sites

does php say anything? error out anything? have you tried using:

 

if ($handle = opendir("./portfolio/2ded/"))

 

maybe opendir can't accept Constants.

 

try to debug the code, echo out variable at certain places to check if the output is what you expected.

 

if the ONLY difference is the location and the fact your using a constant then its either permissions or the constant itself.

Link to comment
Share on other sites

Not sure if it has to do with the server hosting or not but when picking the directory I don't have "/' or "." in it.

You have

 

define(GALLERY_ROOT, "./portfolio/2ded/");

 

where I would have

 

define(GALLERY_ROOT, "portfolio/2ded/");

 

Not sure if that is any help or not..

 

Stephen

Link to comment
Share on other sites

you would use the './' if you wanted to go backwards in a directory.

For example, if the script was running in root/script/ and you wanted it to access root/images/file.png you could use:

$file = './images/file.png';

 

So Stephen is right. You should remove the ./

Link to comment
Share on other sites

incorrect, you would use '../' to base the directory from the parent directory.

 

 

take this:

 

Parent Folder

  Folder A

    test2.php

  Folder B

    test.php - include("../Folder A/test2.php"); would work.

-------

of course i am referring to windows systems i have no experience with unix but i believe its the same

Link to comment
Share on other sites

incorrect, you would use '../' to base the directory from the parent directory.

You are quite corrct, but I wasn't referring to going to the base directory. I was showing that using './' would go back by one directory (I just used the root as an example). As you've said, if you wanted to go to the base directory (instead of just back one folder) you would use '../'

Link to comment
Share on other sites

lol, i really dont mean to be rude, but im not sure its explained correctly, "./" means the current directory the script or page is in (try it in a static html page), whereas "../" would go back one (or up one) directory.

 

 

i always use "./" on every include i use thats from the currnt directory ie;

 

ROOT

  Folder 1

  Folder 2

    scriptA.php - include("./Folder 2B/Folder 2C/scriptB.php");

    Folder 2B

      Folder 2C

      scriptB.php

 

this would work as it works in all of my scripts. try it in static html.

 

 

FYI just a "/" without a period "." would start from the ROOT directory (Directory_ROOT specified in httpd.conf if apache)

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.