r00t0101 Posted May 25, 2009 Share Posted May 25, 2009 Hello all, I am having a difficult time trying to download a file from a directory. I have tried to research this issue but each time I keep getting an empty text file instead of a ~2mb file. What I would like to have is to place my file (new.txt) in a separate folder. As of now it is under the root directory and it works fine but when I include a path then I get an error file not found. Thanks ######################################### ######################################### $file='/path/new.txt'; $type='text/plain'; header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); header("Content-Type: ".$type); header("Content-Disposition: attachment; filename=\"".basename($file)."\";" ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($file)); readfile($file); Quote Link to comment Share on other sites More sharing options...
kevinkorb Posted May 25, 2009 Share Posted May 25, 2009 What happens when you do this? $file='/path/new.txt'; $type='text/plain'; if(file_exists($file)) { header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); header("Content-Type: ".$type); header("Content-Disposition: attachment; filename=\"".basename($file)."\";" ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($file)); readfile($file); } else { die("FILE $file does not exist"); } Quote Link to comment Share on other sites More sharing options...
r00t0101 Posted May 25, 2009 Author Share Posted May 25, 2009 I get file does not exist. Quote Link to comment Share on other sites More sharing options...
kevinkorb Posted May 25, 2009 Share Posted May 25, 2009 That's your problem, it's not finding your file at: $file='/path/new.txt'; where is your path pointed to? It is being pointed from the server root and not your site root correct? Quote Link to comment Share on other sites More sharing options...
r00t0101 Posted May 26, 2009 Author Share Posted May 26, 2009 ah ok, I need this to work from the site root instead of the server root. Is there a way to force it to use the site root? Quote Link to comment Share on other sites More sharing options...
kevinkorb Posted May 26, 2009 Share Posted May 26, 2009 Just use the full path to the site root, or use the relative path. So if the file is in the same directory as your php script calling it just use read("myFilename.txt"); Or if it's in the parent directory... read("../myFielname.txt"); Get it? If not let me know where your file is in repect to the site root and where your php file is in respect to the server root and I'll tell you what to do. Quote Link to comment Share on other sites More sharing options...
r00t0101 Posted May 26, 2009 Author Share Posted May 26, 2009 excellent, thank you for the help. My layout looks similar to the layout below. Within the example.com domain is the download.php and the file new.txt is in the download folder. /root /public_html -> /example.com -> /download.php -> /download -> new.txt Quote Link to comment Share on other sites More sharing options...
kevinkorb Posted May 26, 2009 Share Posted May 26, 2009 ######################################### ######################################### $file='download/new.txt'; $type='text/plain'; header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); header("Content-Type: ".$type); header("Content-Disposition: attachment; filename=\"".basename($file)."\";" ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($file)); readfile($file); Quote Link to comment Share on other sites More sharing options...
r00t0101 Posted May 26, 2009 Author Share Posted May 26, 2009 excellent, thank you kevinkorb! Ah yea I thought the /download would point to the site_root instead of the server_root. Quick question, Is it possible to create a global site_root variable for this? for example: /root /public_html -> /example.com -> /download.php -> /download -> new.txt Could this be done. I would create a global variable named base_dir=/example.com and use $base_dir/download. My goal is to re-do the code, clean it up a bit and to re-organize the source and files into more meaningful names. By cleaning up and updating the code with global variables this would make the source more manageable. so with your update I would like if this could be done. This is probably a bad example but if lets say for example your adding global variables to a source file full of image links where instead of changing all paths you just need to change it in once at the top. base_dir=/example.com $file='$base_dir/download/new.txt'; $type='text/plain'; header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); header("Content-Type: ".$type); header("Content-Disposition: attachment; filename=\"".basename($file)."\";" ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($file)); readfile($file); Quote Link to comment Share on other sites More sharing options...
r00t0101 Posted May 26, 2009 Author Share Posted May 26, 2009 sorry, the question is meant towards multiple sub-domains; Is there a config file that I can create to set the site_root sorta like a jail so it does not look at the server_root? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.