Forum Moderators: coopster

Message Too Old, No Replies

Directory is not echo'd

Using getcwd and scandir not working

         

mvaz

11:18 pm on Dec 18, 2008 (gmt 0)

10+ Year Member



Hello, I am trying to work on a photo album and working on the script below. My images are located within a subdirectory of photos. However, when I run this script, the subdirectories are not echo'd as links (array of links).

Please could the experts in here review my below script and see where I am going wrong.

<?php
$path = getcwd();
foreach(scandir("$path/photos/") as $album) {
if(is_dir($album)) {
print "<a href=\"view.php?album=$album\">"; //This is where the images would be shown
print str_replace("-", ' ', "$album"); //Simply replacing the hiphens in the subfolder when it is echo'd.
print "</a>\n";
}
}
?>

**The entire path for the files would be d://website/photos/folder-with-images/

Any help in any form is highly appreciated.

Many thanks - Melwyn

Sekka

4:37 pm on Dec 19, 2008 (gmt 0)

10+ Year Member



Will you not need to do,

if (is_dir("$path/photos/".$album)) { 

?

mvaz

9:18 pm on Dec 19, 2008 (gmt 0)

10+ Year Member



That's worked like a charm @Sekka, Many thanks

mvaz

9:43 pm on Dec 21, 2008 (gmt 0)

10+ Year Member



But now got a new problem; the script below shows the directories incorrectly; ie. the slashes are shown in reverse; I expect slashes to be forward, but they are backwards.

<?php
$album = $_GET['album']; //from previous page
$path = getcwd(); //current working directory
chdir ('photos'); //directory for photos or albums
$path = getcwd(); //the new directory
echo getcwd(); //showing the current directory
foreach (glob("$path/$album/*.JPG") as $imgfile) {
echo "<img src='$imgfile' />" ."\n";
}
?>

Any ideas on how to get these slahses in the right way?

Many thanks for any help and assistance.

Kind regards

Melwyn

Sekka

11:28 pm on Dec 21, 2008 (gmt 0)

10+ Year Member



If you are testing on Windows then you will get backslashes instead of forwardslashes.

When you run this on Linux it will be forwardslashes.

You could always do a str_replace() [uk3.php.net] to fix it if it is bugging you that much :)