tags:

views:

271

answers:

5

How to access and display the images that are located outside the application folder? Let's say I have a file directory that stores all the images and other file types, and this file directory is located on a different drive then my application folder. For certain file types ( such as .pdf, .doc, I want to prompt the user to download those files).

As far as I know, it's not possible to put absolute path in the <img src attributes, i.e, img src="D:\\file directory"> doesn't work. And also I want to restrict the access to login user only.

I am using PHP as my server side language.

Note: <img src="file:/// .../> won't work in Firefox. See here for comments.

Edit: Since only authenticated user can access those images, and since I have an index.php that checks for all the incoming requests, filters off non-authenticated users, I don't think creating an Apache Alias will work.

Here's an example of my conf file:

  NameVirtualHost *:80
  <VirtualHost *:80 >
    ServerName pmmenu.businessjob.net
    DocumentRoot "D:\web"
    DirectoryIndex index.html index.php
    <Directory  "D:\web">
       AddDefaultCharset UTF-8
       Order Deny,Allow
       Allow from all
       RewriteEngine on
       RewriteBase /
       RewriteRule ^$ index.html [QSA]
       RewriteRule ^([^.]+)$ $1.html [QSA]
       RewriteCond %{REQUEST_FILENAME} !-f

       # no, so we redirect to our front web controller
       RewriteRule ^(.*)$ index.php [QSA,L]
    </Directory>
  </VirtualHost>
A: 

Hi, You could use someting like <img src="file:///C:/app_directory/image.png">

Razvan Stefanescu
I don't think your idea will work:http://kb.mozillazine.org/Links_to_local_pages_don't_work
Ngu Soon Hui
It works for me (FireFox 3.0.11).
Razvan Stefanescu
I guess it works because both the page and the image file are local; if the page would be on a web server then it would not work
Razvan Stefanescu
+1  A: 

try creating a virtual directory of the folder that holds the images and use that as away of linking to the folder. If using IIS (a virtual directory) if apache (a virtual host). There are lots of tutorials on google

A: 

I can't test either of these theories at the moment, but I'm pretty sure you could link the img src to a php script that either:

  • Uses GD to make a dynamic version of the image, or
  • downloads it to the browser in the same way you'd force-download a file.
da5id
either using gd and download to browser... any url or code? Thanks.
Ngu Soon Hui
see my updated answer
middus
+5  A: 

I think you're missing a basic concept here. All HTML is interpreted client-side. Every image is downloaded seperately by the browser of your visitor. As your visitors can only access those files that are public, "file:///something" will not work for them, because this asks their browser to access the local (their) filesystem, which will either be blocked or not find the requested image as it is not stored on their disk.

So you will have to publish those pictures and make them accessible via http to allow your visitors to see them. You could do this by creating a subdomain or a virtual directory.

Since you want only authenticated users to be able to see them, you could also create a php file that checks whether the user is authenticated, e.g. like this:

if(user_is_authenticated()){
    $pathToFile = "d:\images\myimage.jpg";
    $handle = @fopen($pathToFile, "rb");
    $contents = @fread($handle, filesize($pathToFile));
    @fclose($handle);
    //sent appropriate header
    header('Content-type: image/jpeg');
    echo $contents;
}

But you have to be aware that this script will always read the whole image and print it subsequently, which is a huge overhead.

middus
See also this question: http://stackoverflow.com/questions/1385964/how-to-get-the-browser-to-cache-images-with-php
middus
+1  A: 

It's always bad idea to serve files from php. If you can install nginx before apache use http://kovyrin.net/2006/11/01/nginx-x-accel-redirect-php-rails/lang/en/

Otherwise use apache module: http://john.guen.in/past/2007/4/17/send%5Ffiles%5Ffaster%5Fwith%5Fxsendfile/

Another solution - create individual symlinks (as bonus you'll be able to give date limited links, clearing symlinks in cron).

Kane
Access is still authenticated, but there is a new header that php can used to 'hand off' the file to the web server.
txyoji