Connecting Tech Pros Worldwide Help | Site Map

Newbie -- pass document.location to php

Newbie
 
Join Date: Aug 2009
Posts: 3
#1: Aug 26 '09
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.location from index.html, something like this:
<a href="./php/getimages.php?document.location" ....
(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's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#2: Aug 27 '09

re: Newbie -- pass document.location to php


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

Am I understanding you wrong?



Dan
Newbie
 
Join Date: Aug 2009
Posts: 3
#3: Aug 27 '09

re: Newbie -- pass document.location to php


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:

Expand|Select|Wrap|Line Numbers
  1. function getcallingdir(){
  2.  
  3.   $callingdir= strstr($_SERVER[HTTP_REFERER],$_SERVER[HTTP_HOST]);
  4.   $callingdir = str_replace($_SERVER[HTTP_HOST],$_SERVER[DOCUMENT_ROOT],$callingdir);
  5.   $callingdir = trim(str_replace("index.html","",$callingdir));
  6.  
  7.   return $callingdir;
  8.  }
  9.  
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
#4: Aug 31 '09

re: Newbie -- pass document.location to php


Expand|Select|Wrap|Line Numbers
  1. function get_calling_dir() {
  2.     $match = array();
  3.     if (preg_match("/([^/]+)(/index\.html)?$/", $_SERVER['HTTP_REFERER'], $match)) {
  4.         return $match[1];
  5.     }
  6.     return false;
  7. }
Do let me know if it doesn't work.
Newbie
 
Join Date: Aug 2009
Posts: 3
#5: Sep 14 '09

re: Newbie -- pass document.location to php


Works great thanks so much...
Reply

Tags
javascript, path name, php, request_uri