Include file in sub-directory that accesses file in same direcrory

Go To StackoverFlow.com

1

I have a PHP script (index.php) that includes an index.html file in a sub-directory (site1.com). This index.html file displays images that are located in the same directory (site1.com).

Right now, the index.php file includes the index.html file. The html from index.html is shown, but the images are not. The index.html file is looking in the ROOT directory for the images to display. This is the problem. I'd like to somehow include all contents (??) of the site1.com directory, not just the single 'index.html' file.

I've tried changing the working directory, but have had no success. Maybe I'm just not using it correctly...

For reasons that I'd rather not go into explaining, I do not want to edit any configuration settings in PHP or Apache. I also would not like to modify the code of index.html. Basically put, I need to find a solution that can be 100% implemented in PHP. The url needs to appear as though the user is still in the root directory (mysite.com) when, in reality, the content from the sub-directory (site1.com) is being shown. This is sort of like a transparent proxy, in a way.

 [FILE] index.php
 [DIR]  site1.com
   [FILE]  index.html
   [FILE]  img1.png
   [FILE]  img2.png
 [DIR]  site2.com
   [FILE]  index.html
   [FILE]  img1.png
   [FILE]  img2.png

I have done plenty of searching, but have not found a working solution.

2012-04-04 03:09
by Andrew Paglusch


1

When you include()/require() a file, it's as if the contents of that file were literally cut/pasted into the script doing the including. If index.html uses images in the site1.com directory, you'll have to refer to those image using paths based on the location of index.php.

The remote user can NOT tell that you've included a file from a subdirectory on your site - they'll just see some html coming back from your server, so if your index.html has something like

<img src="img1.png" />

in it, but that html is being displayed from the index.php script, then the browser is going to be looking for

http://example.com/img1.png

not

http://example.com/site1.com/img1.png

It's up to YOU to output proper paths.

2012-04-04 03:45
by Marc B
But wouldn't moving the working directory into the folder and THEN including the index.php file do what I want - Andrew Paglusch 2012-04-04 03:58
No. Working directory is purely a server-side concept and has absolutely no bearing on how the client browser interprets the html. If your code outputs <img src="pic1.png" /> then that's what the browser will try to load, regardless of any directory manipulations you've performed on the server - Marc B 2012-04-04 04:01
Could I possibly use cURL (from the 'index.php' file located in the root directory) to connect to my server and load the index.html file from the 'site1.com' sub-directory? And THEN, send the loaded content back to the client, appearing as if the content came from the root directory - Andrew Paglusch 2012-04-04 04:08
no. there'd be no difference between including and curling in this case. Remember, the client browser sees only HTML. it does not see ANYTHING of what you're doing on the server, and has no idea you're loading content from a different directory. Stop looking at it from what your server is doing and start looking at it from what the client is expecting. It wants a valid url pointing at a resource on your server that exists. Right now, you're sending invalid links - Marc B 2012-04-04 04:10
Thanks for your help Marc. I know that I may have come across as quite a "noob". I do understand the client-side and server-side concept. I guess I've just been working on this for too long and have forgotten some basics :) It looks like I'll need to end up modifying the html in index.html after all - Andrew Paglusch 2012-04-04 04:18
Ads