Jump to content

Why will this PHP script work on one page, but not another?


pufferz

Recommended Posts

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 :P

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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 :P...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?

Link to comment
Share on other sites

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?

 

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.