Newbie -- pass document.location to php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • roadiee
    New Member
    • Aug 2009
    • 3

    Newbie -- pass document.location to php

    I am sure this must be common but I can't seem to find how to do it.

    I have a file structure as follows:
    php
    - getimages.php
    gallery
    - index.html
    - image1.jpg
    - image2.jpg
    - image3.jpg

    inside of index.html I have
    <a href="http://bytes.com/submit/304/ ../php/getimages.php"> View images</a>

    what I want to do is in getimages.php be able to set the directory to ./gallery

    Naturally I can hard code this but I would like to have a generic solution just in case I move my php folder, or have a more complex directory structure.

    I thought i could solve this in two ways
    1) send document.locati on from index.html, something like this:
    <a href="./php/getimages.php?d ocument.locatio n" ....
    (notice I have index.html, not a php file and I can't change this)
    2) in getimages.php somehow determine where the request is coming from, maybe use REQUEST_URI

    But I am sure there must be a more elegant approach.
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    I still don't see your reasoning for why you wouldn't just redirect to that folder, but use php header("Locatio n: gallery/").

    Am I understanding you wrong?



    Dan

    Comment

    • roadiee
      New Member
      • Aug 2009
      • 3

      #3
      Sorry I am not sure I understand what you are asking ( or perhaps how header works) but my issue is that I also want to call the same file getimages.php and it needs to load the files from the location of the calling index.html.

      So if index.html is in ../gallery/ then run get images from ../gallery. If index.html is in ../gallery2/ then it should get the images from gallery 2 etc.

      So my side structure is more complex than this, and looks more like:
      php
      - getimages.php
      2002
      - 01gallery
      - index.html
      - image1.jpg
      - image2.jpg
      - 01gallery
      - index.html
      - image1.jpg
      - image2.jpg
      - 02gallery
      - index.html
      - image1.jpg
      - image2.jpg
      - 04gallery
      - index.html
      - image1.jpg
      - image2.jpg
      etc (for 2003, 2004, ...)

      Does that make sense?

      I did write this bit of code to get it to work but it seems a bit of an overkill:

      Code:
      function getcallingdir(){
       
        $callingdir= strstr($_SERVER[HTTP_REFERER],$_SERVER[HTTP_HOST]);
        $callingdir = str_replace($_SERVER[HTTP_HOST],$_SERVER[DOCUMENT_ROOT],$callingdir);
        $callingdir = trim(str_replace("index.html","",$callingdir));
          
        return $callingdir;
       }

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        Code:
        function get_calling_dir() {
            $match = array();
            if (preg_match("/([^/]+)(/index\.html)?$/", $_SERVER['HTTP_REFERER'], $match)) {
                return $match[1];
            }
            return false;
        }
        Do let me know if it doesn't work.

        Comment

        • roadiee
          New Member
          • Aug 2009
          • 3

          #5
          Works great thanks so much...

          Comment

          Working...