Dean219 Dean219 - 2 months ago 6
PHP Question

Problems accessing file using database string

I am having a problem accessing a file on my web server after restructuring the routes. After restructuring the files to download are stored in the address :


Previously it was stored in:


and so the path /uploads/test.txt would work when reading from the database. Iv recently updated the routes as visible in the first sample and have updated the content in the mongodb database. However as the uploads folder is in root and the download.php file is in the submission folder the path from the database im assuming points to:


in my database it contains the text to the path exactly as follows:


I tried to append the prefix to the string to point up a directory back to root, but it does not function:


This seems to result in it including the '..' instead of pointing it to the root directory.

Here is the download function which gets passed the path from the database:

function download($path){

header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.basename($path));
header('Content-Type: application/octet-stream');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
if (isset($_GET['download'])) {
if (!empty($_GET['download'])) {
$file = $_GET['download'];

Any help would be appreciated.


Alright, glad we finally got it sorted! A common practice is to define a ROOT/ROOT_PATH/etc constant that you can reference globally for all inclusions like you require. This is usually done in the root index.php file with:

DEFINE('ROOT', getcwd());

Now, there are a few issues in your current function definition. You need to supply the arguments if you want to use them. You can resolve this by amending it to:

function download($file) { ...
                  ^^^^^--- we added this.

Now, when defining your file, you need to specify the absolute path:

$path = ROOT . DIRECTORY_SEPARATOR . $my_path_from_db;

And you should note that DIRECTORY_SEPARATOR is an internal PHP defined constant which represents \ or / depending on operating system :).