使用数据库字符串访问文件有问

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 :

http://mysite/submission/uploads/test.txt

Previously it was stored in:

http://mysite/uploads/test.txt

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:

http://mysite/mydownloadpage/submission/uploads/test.txt

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

submission/uploads/test.txt

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

../submission/uploads/test.txt

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){

if(!file_exists($path)){
die('Error');
}else{
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));
ob_clean();
ob_start();
flush();
readfile($path);
exit;
}
}
if (isset($_GET['download'])) {
if (!empty($_GET['download'])) {
$file = $_GET['download'];
download($file);
}
}

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 :).