Hi all,
I am working on a PHP project. Im having a problem giving the 'src' for a image, because the image file is placed outside of the www folder :(.

Let me explain more.

When I log in to my server using FTP logins, it gives the directory structure as below

+--www
+--project1
-----+www
----------index.php
-----+resources
--------+additional
---------------image.jpg
+--oldproject
+(some other folders)

I can access my project1 directly through the browser using

That means /project1/www/index.php file directly get executed by calling (I really don't know how it is done, but it's working :))

Now I want to use project1/resources/additional/image.jpg for a <img> tag in /project1/www/index.php file.

I tried, <img src="../resources/additional/image.jpg"> But it's not woring :(

Can anybody help me with this please...?

Thank you so much for spending time to read this

Cheers
Niranga

Dani AI

Generated

What you are seeing is expected: browsers can only request URLs that your web server maps under its document root. A URL like ../resources/... is resolved relative to the URL path, not your server’s filesystem, and it will never escape the host root (so it collapses to /resources/... under http://sitename.com/). Files outside the configured web root are invisible to the browser unless you explicitly expose them.

You have three safe options:

  • Map a public URL to that external folder at the web-server level. For Apache, an Alias makes the folder reachable without moving it:

    Alias /resources/ /home/users/web/b562/whl.aunn2008/project1/resources/
    <Directory "/home/users/web/b562/whl.aunn2008/project1/resources/">
    Require all granted
    Options -Indexes
    </Directory>

    For nginx:

    location /resources/ {
    alias /home/users/web/b562/whl.aunn2008/project1/resources/;
    autoindex off;
    }

    Docs: Apache Alias, nginx alias.

  • Keep files outside the web root and stream them through PHP after validating the path, setting the MIME type via finfo and appropriate headers, and enforcing a whitelist to prevent traversal. For performance at scale, consider offloading with X-Sendfile/X-Accel-Redirect (mod_xsendfile, nginx X-Accel-Redirect). See PHP’s readfile and Fileinfo docs: readfile, Fileinfo.

  • If allowed by your host, create a symlink within the web root that points to the external folder, but confirm server settings (FollowSymLinks) and permissions.

Recommended Answers

All 11 Replies

My guess is that you're using the first www folder not the www folder that lies under the "project1" folder

so I guess you should try:
../project1/resources/additional/image.jpg

Hi theighost,

Thanks a lot for the reply..!!!
Actually I tried that too... but it didn't work :(

To check which www folder is been used, I did some changes to www/index.php( wrote a single line to print "test"), but nothing happened.
So again I put the same line of code into the /project1/www/index.php and it printed that line

Please help me out.
This project originally done by another developer and now i can't contact him :(

Cheers Niranga

hello,

Try echoing "$_SERVER" and see where is the page exactly..

if the image was in the correct place and the .php file was in the correct place too, then "../resources/additional/image.jpg" would work no doubt about that.

Hi,
So Sorry for taking so much time to reply.

$_SERVER gives me this
/home/users/web/b562/whl.aunn2008/project1/www/

So as the FTP client displays the folder structure the image should be placed in /home/users/web/b562/whl.aunn2008/project1/resources/additional/image.jpg

Still ../resources/additional/image.jpg does not displays the image :( :( :(

Niranga

could you check if the image is there..

Why don't you just move the folder? lol

Also, I may be wrong, but can't you use multiple .. 's? So something like this..


../../../folder/file.txt

Hiii,

: Yes I double checked it. Unfortunately image is there.... :)

@CodeEgg: Yes, if i could move the folder it will be great, but the poblem is that folder is been used by some other functions also :(. I tried using multiple '../' too. but no luck.

If http://sitename.com/ refers directly to the www folder, is it possible to refer another folder which placed outside of the www folder(in same level) ?

For an example let's say file contains a link like below. <a href="help.html">HELP</a> When I move my mouse pointer over it, i can see the destination address as

If I insert the following link, <a href="../resources/temp.html">TEMP</a> and move my mouse pointer over it, i get the destination address as

So does that mean, '../' do not go out from the www? :( :(

Cheers
Niranga

Hi,
So Sorry for taking so much time to reply.

$_SERVER gives me this
/home/users/web/b562/whl.aunn2008/project1/www/

So as the FTP client displays the folder structure the image should be placed in /home/users/web/b562/whl.aunn2008/project1/resources/additional/image.jpg

Still ../resources/additional/image.jpg does not displays the image :( :( :(

Niranga

OK, by what you've said, the webserver is serving pages from your site out of this folder:

/home/users/web/b562/whl.aunn2008/project1/www/

So, anything OUTSIDE that folder will not be visible to your web browser. It sounds like you want to move the resources folder into this www directory, so the full path to the image will be:

/home/users/web/b562/whl.aunn2008/project1/www/resources/additional/image.jpg

And for your web browser the path would be:

If you want to keep the images in this other folder, you'll need to use server side scripting of some kind to include the image in your page.

-----------------------------------------------

After reading the rest of the thread, I see that you can't move the images. So, there's probably some function that is serving the images already that you can use, or write your own, something like this:

In a file called "image.php":

<?php

$file = basename(urldecode($_GET['file']));
$fileDir = '/home/users/web/b562/whl.aunn2008/project1/resources/additional/';

    if (file_exists($fileDir . $file))
    {
        // Note: You should probably do some more checks 
        // on the filetype, size, etc.
        $contents = file_get_contents($fileDir . $file);

        // Note: You should probably implement some kind 
        // of check on filetype
        header('Content-type: image/jpeg');

        echo $contents;
    }
?>

In the index.php file:

<img src="image.php?image.jpg">

Hi genevish,

Thanks a lot for your reply sir. I will try the code you posted and get back to you as soon as possible..

Thanks again..!!!

Cheers
Niranga

Hi genevish,

Thaks a lot genevish....!!!! :) :) :) :)

Its working...!!!!
Great solution..!!!! Thanks a lot...!!! I really appreciate it..

Thanks again..

Cheers...!!!!!
Niranga

Good Knowledge keep it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.