Accessing Files Outside the Document Root with Apache

Accessing files outside the document root with Apache

You can create a directory alias:

<VirtualHost....>

..... stuff .....

Alias /mydir /a/b/c

</VirtualHost>

then you could access the text file like so:

domain.com/mydir/myfile.txt

Note that this needs to take place in the central configuration file, not a .htaccess file.

Symlinks are an option too. Make sure you have

Options +FollowSymlinks

turned on.

PHP get file outside of document root

First make sure apache as permission to read the files on /var/extra-files, then you can use:

require "/var/extra-files/file.php";

You may want to read Difference between require, include and require_once?

Files outside of Apache document root

After some hunting, I found my own answer. In my situation, the alias wasn't located in httpd.conf, but in extra/httpd-xampp.conf:

Alias /phpmyadmin "C:/xampp/phpMyAdmin/"
<Directory "C:/xampp/phpMyAdmin">

How to access files outside of root directory in localhost?

You cannot access files outside your htdocs folder because your server considers that to be the root folder. Hence has no parent folder. Or in basic terms, your apache server is only available in your htdocs folder. So you might have to move your files there or give a link to them online.

Conclusion: There is no way you can do that.

Or maybe you have to edit your httpd.conf file

How do I access files placed outside of the site root?

Have a look at the answers to this question, which seem to be doing more or less the same thing.

A quick summary: readfile() or file_get_contents() are what you're after. This example comes from the readfile() page:

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>

I don't recommend allowing the $file variable to be set using user input! Think about where the filenames are coming from before arbitrarily returning files in the response.



Related Topics



Leave a reply



Submit