pufferz Posted April 16, 2009 Share Posted April 16, 2009 I'm trying to implement the provided script into a wordpress blog. I'm using execPHP to allow me to use PHP inside of the pages of the blog. The script will take a directory name, then randomly select 6 pictures from that directory and outputs the HTML to display those images. The script works perfectly for the home page of my blog but when I try to apply it to one of the peripheral pages it will not display, even if I point them to the same directory. This is the first segment, placed at the top of the blog: <?php function getRandomFromArray($ar) { mt_srand( (double)microtime() * 1000000 ); $num = array_rand($ar); return $ar[$num]; } function getImagesFromDir($path) { $images = array(); if ( $img_dir = @opendir($path) ) { while ( false !== ($img_file = readdir($img_dir)) ) { // checks for gif, jpg, png if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) { $images[] = $img_file; } } closedir($img_dir); } return $images; } ?> And then this segment is placed where I need the pictures: <?php $path = 'Images/'; $imgList = getImagesFromDir($path); $img = getRandomFromArray($imgList); $img2 = getRandomFromArray($imgList); $img3 = getRandomFromArray($imgList); $img4 = getRandomFromArray($imgList); $img5 = getRandomFromArray($imgList); $img6 = getRandomFromArray($imgList); echo"<img src=\"" . $path . $img . "\" alt=\"Wallpaper Preview\" />"; echo"<img src=\"" . $path . $img2 . "\" alt=\"Wallpaper Preview\" />"; echo"<img src=\"" . $path . $img3 . "\" alt=\"Wallpaper Preview\" />\n"; //break echo"<img src=\"" . $path . $img4 . "\" alt=\"Wallpaper Preview\" />"; echo"<img src=\"" . $path . $img5 . "\" alt=\"Wallpaper Preview\" />"; echo"<img src=\"" . $path . $img6 . "\" alt=\"Wallpaper Preview\" />"; ?> So for example, if I call the script at www.myblog.com it works, but if I try to call it at www.myblog.com/?page_id=11 it doesn't. Even when I hard code the path to the directory, such as http://www.myblog.com/images/summerphotos/ it won't work (for either page actually). All help would be greatly appreciated! I've been stuck on this seemingly simple project for almost two days now, and I'd like to get moving again Quote Link to comment Share on other sites More sharing options...
keeB Posted April 16, 2009 Share Posted April 16, 2009 As an aside.. You need to clean up your code. <?php $img_amt = 6; for ($i = 0; $i < $img_amt; $i++) { $img = getRandomFromArray($imageList); echo "<img src=\"$path/$img\" alt=\"Wallpaper Preview\" />"; } ?> Unless, you know, you're silly Quote Link to comment Share on other sites More sharing options...
pufferz Posted April 16, 2009 Author Share Posted April 16, 2009 As an aside.. You need to clean up your code. <?php $img_amt = 6; for ($i = 0; $i < $img_amt; $i++) { $img = getRandomFromArray($imageList); echo "<img src=\"$path/$img\" alt=\"Wallpaper Preview\" />"; } ?> Unless, you know, you're silly We'll I do consider myself a pretty silly guy. Thanks for the advice though. This is the first thing I ever coded in PHP...and after this experience, hopefully my last ...I'll stick with my hard coded languages. I was pretty proud of my additions to the code though! But again, thanks for the advice. Hopefully I'll be able to get this working soon. Some developments and speculations: I have a feeling my problem lies with the file privileges of the pages of my blog. My home page, where the script actually works, is probably in the root of my webhost. However, my peripheral pages seem to have altering PHP parameters attached to them (?page_id=11 are PHP parameters if I'm not mistaken). Unfortunately, I have absolutely no idea how to solve this problem them...aren't www.myblog.com and www.myblog.com?page_id=11 technically the same page, with equivalent file permissions? Quote Link to comment Share on other sites More sharing options...
keeB Posted April 16, 2009 Share Posted April 16, 2009 Your speculation may be correct. Now it's time to prove it! Use http://us.php.net/manual/en/function.getcwd.php to find out where you are. Quote Link to comment Share on other sites More sharing options...
pufferz Posted April 16, 2009 Author Share Posted April 16, 2009 Your speculation may be correct. Now it's time to prove it! Use http://us.php.net/manual/en/function.getcwd.php to find out where you are. Nifty. Using the code provided on that page: <?php // current directory echo getcwd() . "\n"; chdir('cvs'); // current directory echo getcwd() . "\n"; ?> the myblog.com page 'echos' out as: /home/www/myblog.com /home/www/myblog.com and the myblog.com?page_id=11 'echos' out as: /home/www/myblog.com /home/www/myblog.com Unfortunately, it looks as though I'm working in the same directory. So theoretically, shouldn't the scripts work in precisely the same manner? I'm quite baffled here :-\ The only difference that I can find is '?page_id=11' appended to all of the peripheral pages. Also, is there a reason why I hardcoding the HTTP path to the directory isn't working? Shouldn't passing a request to fetch a directory work the same way as a request to fetch, say, and image? Quote Link to comment Share on other sites More sharing options...
pufferz Posted April 16, 2009 Author Share Posted April 16, 2009 Update! Success...apparently directory names are case sensitive in PHP? Who knew? Quote Link to comment Share on other sites More sharing options...
keeB Posted April 16, 2009 Share Posted April 16, 2009 Now you do! Stick with it, it's pretty fun, even if there are more elegant languages 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.