PHP includes refererencing files from different directory thread

NamecheapNamecheap
Watch

FreeBaGeL

Established Member
Impact
2
PHP includes refererencing files from different directory

Ok, here's the deallio.

I've got several domains, some of them reference the same files. For example's sake, we'll call the current directory I'm working in "current."

The directory structure has all the domains on the same level, so I use for instance, the same footer file in each domain. Just say to keep things simple in this case that Util is a domain on the same level as all the others.

So, I've just been referencing it with for instance

Code:
include "../../Util/public_html/footer.html";

This works fine. However, I've run into an issue when I try to include a file that contains other files (in particular images) that it refers to.

For instance I have a file called "navHeaders.html" that calls a few images inside of it. These images are in the same dir as navHeaders, the Util dir. So I have

Code:
include "../../Util/public_html/navHeaders.html";

This calls the navHeaders file just fine, but all of the images are broken since it's looking for them in the dir I'm working in (current) even though the file that's calling them is in a different directory. navHeaders calls simply "image.jpg" Shouldn't it look for this in the directory navHeaders is in? Is there any way to get around this without having to upload all the images into my current working directory (since that defeates the purpose of all these different domains referrincing the same file anyway)?

Thanks in advance.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
FreeBaGeL said:
This works fine. However, I've run into an issue when I try to include a file that contains other files (in particular images) that it refers to.

For instance I have a file called "navHeaders.html" that calls a few images inside of it. These images are in the same dir as navHeaders, the Util dir. So I have

Code:
include "../../Util/public_html/navHeaders.html";
In your example, the navHeaders.html file would actually be located in the public_html directory and not the Util dir.

FreeBaGeL said:
This calls the navHeaders file just fine, but all of the images are broken since it's looking for them in the dir I'm working in (current) even though the file that's calling them is in a different directory. navHeaders calls simply "image.jpg" Shouldn't it look for this in the directory navHeaders is in? Is there any way to get around this without having to upload all the images into my current working directory (since that defeates the purpose of all these different domains referrincing the same file anyway)?

Thanks in advance.

Yes it will look in the same directory that navHeaders is in, but from your example this would actually be the public_html directory. So if you had <image src=image.jpg"> in the navHeaders.html file it would look for image.jpg in the public_html directory. You'll need to put files that are directly accessed like this inside a web accessible directory anyway.
 
0
•••
deadserious said:
Yes it will look in the same directory that navHeaders is in, but from your example this would actually be the public_html directory. So if you had <image src=image.jpg"> in the navHeaders.html file it would look for image.jpg in the public_html directory. You'll need to put files that are directly accessed like this inside a web accessible directory anyway.

That was just a typo when making the post, when I said it was in the Util dir I meant the Util/public_html dir.

The navHeaders.html file, as well as the image.jpg file are both in Util/public_html. But when I include navHeaders.html from a dir called, say, "CurrentDir" I'll get an error that CurrentDir/image.jpg cannot be found.

It should be also noted, not sure if I mentioned it in my original post, that the examples we're using of Util and CurrentDir are actually domains, not just directories, but in my ftp client they're handled the same as directories, and all of the domains are on the same level in the hierarchy.
 
0
•••
FreeBaGeL said:
That was just a typo when making the post, when I said it was in the Util dir I meant the Util/public_html dir.

The navHeaders.html file, as well as the image.jpg file are both in Util/public_html. But when I include navHeaders.html from a dir called, say, "CurrentDir" I'll get an error that CurrentDir/image.jpg cannot be found.

It should be also noted, not sure if I mentioned it in my original post, that the examples we're using of Util and CurrentDir are actually domains, not just directories, but in my ftp client they're handled the same as directories, and all of the domains are on the same level in the hierarchy.

Oh I see what you're saying now. That's because when you're including the file from another directory, the html output of the file you're including is simply the same, so it will be calling the image.jpg file from CurrentDir/image.jpg.

You would have to change the path of your image(s) in your html. The easiest way would probably be to just use the full url to your images in the html rather than the relative path.
 
0
•••
deadserious said:
The easiest way would probably be to just use the full url to your images in the html rather than the relative path.

so like "http://www......" ?

Is that ok to do, will it slow down the page loading or anything like that?
 
0
•••
dont use the full url edit you build your pages do this

say you have an image called dave.gif and you want to insert it

use this code
<img src="/images/some/other/path" height=blah width=blah>

so if your file is being included in
/dave/1/3/qwetty/
or
/index.html

it will always work it works because the first slash inside the / tells the browsers to request files from the root directory so it doesnt matter where you place images.
 
0
•••
FreeBaGeL said:
so like "http://www......" ?

Is that ok to do, will it slow down the page loading or anything like that?
Yes, that's okay to do, and it won't slow down the page loading or anything, the only thing is you'd need to change it if you ever moved your site to a new domain or something like that.

What adam_uk suggested would also work if you had a copy of whatever images/files you needed in each domains public_html directory. Because like he said, it will always request the files from the root directory. But the problem is it will be the root directory of that particular domain so you would need a copy of the images in each domains public_html directory. If you only wanted to have one set of images in just one of the domains public_html directories or somewhere else, the easiest way would probably be to use the full url which is why I suggested that way.
 
0
•••
Dynadot — .com TransferDynadot — .com Transfer
Appraise.net

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back