SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Cross-directory inclusion of PHP

    I have a script that displays a bar of randomly chosen images by using include() to include the script on any page that I want to have these random images on. The number of random images displayed depends on the vertical screen size of the user. The pages that include this script are in several different directories and the images that are used by the script are all stored in one directory (randomimgs), which is in the root dir. The script is also in the root dir. This is the code for it:

    PHP Code:
    if (isset($_GET['width']) AND isset($_GET['height'])) {
        
    $xres $_GET['width'];
        
    $yres $_GET['height'];    
    } else {
     
     echo 
    "<script type=\"text/javascript\">\n";
     echo 
    "  location.href=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}"
               
    "&width=\" + screen.width + \"&height=\" + screen.height;\n";
     echo 
    "</script>\n";
     exit();
    //END IF

    //start random images bit
    $list = array();
        foreach (
    glob("randomimgs/*.jpg") as $file
        {
            
    $list[] = $file;
        }

    if(
    $yres >= 1024) {$ni 8;} else {$ni 5;} //no. of images to show

    shuffle($list);

    echo 
    '<div id="photocolumn">';

    $first=1;
    for(
    $i=0;$i<$ni;$i++)
    {
        
    $file $list[$i];
        
    $first=0;
        echo 
    "<img src=\"{$file}\" alt=\"{$file}\" />";
    //NEXT $i

    echo '</div>'
    I have two problems: The first is that the javascript bit to work out the screen size only works if the pages that include the script are located in the root dir. I have a subdir where there are several pages that include the script: I include it by using
    PHP Code:
    include ("$_SERVER['HTTP_HOST']/script.php"); 
    I have to do this because these pages that use the script are in turn included in pages in another subdir. The thing is, when the page is loaded, it doesn't simply include the script in the page, but loads only the script (so I only see a column of photos). I hope this makes sense.


    Problem 2 is much simpler: Because the script is used across multiple directories, the following line has to refer to the dir where the images are stored absolutely:
    PHP Code:
    foreach (glob("randomimgs/*.jpg") as $file
    I tried using the following and many other things, but it doesn't work:
    PHP Code:
    foreach (glob($_SERVER['HTTP_HOST']."/randomimgs/*.jpg") as $file
    I can't work out how to make it non-relative (i.e. not ../randomimgs/*.jpg or simply randomimgs/*.jpg), if you see what I mean.

    I would really appreciate any help, since I am stumped.

  2. #2
    SitePoint Evangelist Will Kelly's Avatar
    Join Date
    May 2005
    Location
    London
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach (glob($_SERVER['DOCUMENT_ROOT']."/randomimgs/*.jpg") as $file
    Is that what you want?

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Will Kelly
    PHP Code:
    foreach (glob($_SERVER['DOCUMENT_ROOT']."/randomimgs/*.jpg") as $file
    Is that what you want?
    $_SERVER['DOCUMENT_ROOT'] is equal to /www/doc_root/ on the server, regardless of the directory. That's pretty meaningless to me.

    However, $_SERVER['HTTP_HOST'] is the top-level domain (as in www.server.com), so I don't understand why $_SERVER['HTTP_HOST']."/randomimgs/*.jpg" should not work, since the directory is located at www.server.com/randomimgs.

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    OK, I've managed to sort out the JS business, but I still can't make anything I put after glob work across directories:

    I'd even be happy if, despite the fact it doesn't work, I knew why it doesn't work, since the code below looks fine to me!
    PHP Code:
    foreach (glob($_SERVER['HTTP_HOST']."/randomimgs/*.jpg") as $file
    (which ought to translate as http://www.myserver.com/randomimgs/image1.jpg, for instance, which is actually the address where the images are stored).


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •