469,358 Members | 1,659 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

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.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.
Aug 26 '09 #1
4 3670
dlite922
1,584 Expert 1GB
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
Aug 27 '09 #2
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.  
Aug 27 '09 #3
hsriat
1,654 Expert 1GB
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.
Aug 31 '09 #4
Works great thanks so much...
Sep 14 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by news-server.tampabay.rr.com | last post: by
1 post views Thread by colleen1980 | last post: by
3 posts views Thread by r_ahimsa_m | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.