xymbo Posted February 18, 2008 Share Posted February 18, 2008 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 Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 18, 2008 Share Posted February 18, 2008 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. Quote Link to comment Share on other sites More sharing options...
Stephen68 Posted February 18, 2008 Share Posted February 18, 2008 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 Quote Link to comment Share on other sites More sharing options...
Dragen Posted February 19, 2008 Share Posted February 19, 2008 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 ./ Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 19, 2008 Share Posted February 19, 2008 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 Quote Link to comment Share on other sites More sharing options...
Dragen Posted February 19, 2008 Share Posted February 19, 2008 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 '../' Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 19, 2008 Share Posted February 19, 2008 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) Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 19, 2008 Share Posted February 19, 2008 xymbo has any of these solutions proved successfull? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.