Reputation:
im trying to reach an image that is located in a folder outside my site. I tried to go outside my folder by putting ../ but this end with an error. I also tried to give an absolute path but there are spaces in there (like "documents and settings\") and this also doesnt work
does anybody have an idea?
Upvotes: 0
Views: 7620
Reputation: 3875
Have you tried to encapsulate the path in a string? e.g.
cd "C:\Documents and Settings\foo\bar\"
Upvotes: 0
Reputation: 300865
You can't access files outside your document root from the client side HTML. If you can't move the files under the document root, then you can either configure your server to allow access to that one directory, or write a server side script to relay the files.
If you're using Apache, you could use an Alias to map a directory on your filesystem into the document root:
Alias /foo "/path/to/my/foo"
Plan B is to write a script to fetch the files. For example, instead of attempting something like
<img src="/../foo/bar.gif">
You could do this
<img src="/getimage.php?name=bar.gif">
A simple implementation of getimage.php might be something like this
//get the passed filename
$file=$_GET['name'];
//basic security check - ensure the filename is something likely
if (preg_match('/^[a-z]+\.gif+$/', $file))
{
//build path and check it exists
$path=$_SERVER['DOCUMENT_ROOT'].'/../foo/'.$file;
if (file_exists($path))
{
//return file here. A production quality implementation would
//return suitable headers to enable caching and handle
//If-Modified-Since and etags...
header("Content-Type: image/gif");
header("Content-Length:".filesize($path));
readfile($path);
}
else
{
header("HTTP/1.0 404 Not found");
echo "Image not found";
}
}
else
{
header("HTTP/1.0 400 Bad Request");
echo "Naughty naughty";
}
Upvotes: 6
Reputation: 655319
You cannot reference resources outside your document root of your webserver. But you can define an alias for that folder:
Alias /images "C:/Users/Horst/Documents and Settings/My Images/"
Upvotes: 2
Reputation: 25339
Stand HTML can't access files outside of the web root (for very good security reasons). Your best bet to achieve this would be to use a programming language to write some kind of handler that reads the file and then passes it to the output stream. For instance, in ASP.NET you could write an .ASHX handler.
Upvotes: 1
Reputation: 2525
Where is your image located? Generally, a website cannot access the full file system (for obvious reasons) of the computer serving it. You should either put the image in a folder under the root of your website or host it elsewhere.
Upvotes: 3