March 18th, 2012, 11:38 AM
Why do my paths outside my web directory not work?
I have been using php for ages, but not so long with Unix/Linux.
I recently built and began using an Ubuntu server (11.10) and have been happy with it so far.
But I have come across a problem that I am not sure of, and may be related to my lack of Linux knowledge.
In Ubuntu, Apache2, the default wed directory is under /var/www.
I am trying to reference files from one website directory to another, and cannot get it to work for some reason. So I was wondering if it is an illegal operation.
For example I have:
If I try to do this from the blue.com directory for example.
<img src='/var/green.com/files/01234.jpg' />
It will not work.
Is it not possible to reference files outside of your primary web directory?
BTW I asked this in the Ubuntu forums and got stupid answers like use 'www.green.com/01234.jpg', which obviously does not answer the question, and is not at all what I am going to do if I am referencing hundreds of files dynamically.
March 18th, 2012, 12:25 PM
it dpends on the kind of access, you can execute external files through symblinks and exec() pass_thru()
you can parse file using file_get_contents() and file()
you cannot use a php file as a page on your site
March 18th, 2012, 12:26 PM
and php wont do many things when its in safe mode todo with filesystem as it passes the uid checks that is linux chmod etc
March 18th, 2012, 12:59 PM
I ♥ ManiacDan & requinix
This is a sig, and not necessarily
a comment on the OP:
don't be a help vampire
March 18th, 2012, 03:58 PM
You can not use an absolute local filesystem path in HTML.
Is like writing this:
<img src='/var/www/green.com/files/01234.jpg' />
Since the user's browser does not have local filesystem access to your server it cannot possibly look up a remote file using an absolute filesystem path.
<img src='C:\www\green.com\files\01234.jpg' />
Using something like http://www.green.com/files/01234.jpg is actually a correct answer, because Apache will resolve www.green.com to point to the local /var/www/green.com/ directory, and then it will be able to find the file 01234.jpg and serve it to the client.
If you want to be able to access the file through the blue.com domain, then you will have to use mod_alias or mod_rewrite, as ptr2void pointed out. You could create an alias that maps www.blue.com/green.com/files to /var/www/green.com/files/, and then you would be able to use "/green.com/files/01234.jpg" or "http://www.blue.com/green.com/files/01234.jpg" as your img src.
March 19th, 2012, 01:19 PM
Ok I need to make this clearer I think
If I want the php script "display.php" to reference items in the images directory I believe I should be able to use
the "../../" says to step up two directories, which would put me at "/var/www/green.com/".
Then add on "code/images/"
which should put me at "/var/www/green.com/code/images/".
where my php script would display a file $member['photo'], if it found it in the directory.
Now is this wrong? Why does this not work?
March 19th, 2012, 01:23 PM
you have to make a alias for each remote folder, for just a base directory folder
March 19th, 2012, 01:30 PM
Can you expand on that with maybe an example?
Originally Posted by p0rt
March 19th, 2012, 03:54 PM
a folder is a single folder :
a directory would be :
so you can still goto /var/www/green.com/code/images/ and use ../ to go up a level to /var/www/green.com/code/
March 19th, 2012, 04:28 PM
Your client cannot access anything in var. It only can access var/www (or var/http depending on how your server is set up). Try just the following:
<img src='/var/green.com/files/01234.jpg' />
also, within PHP, I recommend using full names as it prevents silly mistakes.
<img src='green.com/files/01234.jpg' />
March 19th, 2012, 06:50 PM
It doesn't work because you're still constructing your paths using filesystem paths.
If the URL http://green.com/sub/display.php contains src='../../code/images/file12345.html' the browser will choke because you're telling it to load "http://green.com/sub/../../code/images/file12345.html". Your web path is /sub; you can't go up two directory levels at this point because your web path is only one directory level long.
"../../" would actually step you into "/var/www". Your web path cannot step above "/var/www/green.com/".
Using mod_alias you could construct an alias under green.com that points to files in the blue.com domain. For example:
This means that if you put "http://www.green.com/blue_images/sub/123.gif" or "/blue_images/sub/123.gif" in your src attribute, the server will return the file /var/www/blue.com/images/sub/123.gif from the filesystem.
Alias /blue_images /var/www/blue.com/images
Although in many circumstances there is pretty much no advantage to doing that vs just writing http://www.blue.com/images/sub/123.gif in your src attribute.
There is no way to configure your server that will allow you to use filesystem URLs in HTML.
Last edited by E-Oreo; March 19th, 2012 at 07:00 PM.