sdfor
sdfor

Reputation: 6448

How to access images on a directory outside of the web application

I'm using Apache and Windows and I'm writing an application that needs to display the images of parts that are on a different server, not in the path of the application directory. There are too many images to move and other applications use these same images.

What is the best way to deal with this problem?

The back end is php and mysql - though I don't think that's relevant.

Thanks

Upvotes: 3

Views: 4629

Answers (4)

sdfor
sdfor

Reputation: 6448

The best answer that I've discovered if your using Apache. which I am, is to use the Apache Alias feature, to allow access to an internal directory.

As suggested by the answer from BGY above.

I added the following to http.conf:

Alias /fabimages p:/IMDATA/IMAGES
<Directory p:/IMDATA/IMAGES>
    Order allow,deny
    Allow from all
</Directory> 

in my code I set the .src attribute to the image I need:

var fabimage = document.getElementById("fabImageTag");
fabimage.src="/fabimages/"+imageName; // and it picks up the image from p:/IMDATA/IMAGES

Upvotes: 0

Boris Gu&#233;ry
Boris Gu&#233;ry

Reputation: 47585

if your files are stored outside the www root. then, you'll need to have enough permissions to access to the files.

Then, you could do something like :

<?php

$imageFileName = $_GET['image'];
$path = '/var/data/somewhere/';

$fullpath = $path . $imageFileName;

if (!is_file($fullpath))
{
   die('Image doesn't exist);
} else {
   $size = filesize($fullpath);
   $content = file_get_contents($fullpath);
   header("Content-type: image/jpeg");
   echo $content;
}

Well, don't use this code in a production environement, since it's NOT SECURE.

You can use getimagesize() to check if it's an image. Blacklist the phps extensions, etc... Specify a working directory, to don't be able to use the backward ../../

file_get_contents()

EDIT :

About your comment about the symbolic link, if you have access to apache.conf file, you can specify an alias which points to another directory outside your webroot.

Upvotes: 2

sjobe
sjobe

Reputation: 2837

Since the images are on a server, why not reference them directly in your code ?

<img src="http://myotherserver.com/images/picture.jpg"

Upvotes: 0

Rap
Rap

Reputation: 7292

Have you looked at symbolic links?

ln -s LocalName FullPathOfRealFileLocation

Upvotes: 0

Related Questions