brdalert Posted November 2, 2008 Share Posted November 2, 2008 Hi, I have a lot of documents that are downloadable on my site. What i' wanting to do is have a jump menu link to individual pages, these pages would be the ones that would auto start the downloads. or a way to download straight from the jump menu would be nice. Any help would be appreciated. Quote Link to comment Share on other sites More sharing options...
marcus Posted November 2, 2008 Share Posted November 2, 2008 <?php ob_start(); $file = $_GET['file']; 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); }else { echo "File does not exist"; } ob_end_flush(); ?> Quote Link to comment Share on other sites More sharing options...
brdalert Posted November 2, 2008 Author Share Posted November 2, 2008 Where do i put the file name. and does the file have to be in the same folder as the script page or can it be in a subdir. Â Quote Link to comment Share on other sites More sharing options...
raman Posted November 2, 2008 Share Posted November 2, 2008 As you can see the $file takes the file by method get.So whatever the name of the file coming in this would be taken,also the file can be anywhere but you must specify the right path in the original form from where you are sending( by get or post) the variable file.Also you must specify appropriate permissions on that file for its download from a particular directory. Quote Link to comment Share on other sites More sharing options...
brdalert Posted November 2, 2008 Author Share Posted November 2, 2008 I keep getting this error. Parse error: parse error, unexpected T_STRING in /home/e/a/4/10671/10671/public_html/test doenload.php on line 12. Sorry this i my first attempt at php. sorry about the page name i was missed typed. Quote Link to comment Share on other sites More sharing options...
brdalert Posted November 2, 2008 Author Share Posted November 2, 2008 This is my Page. Â <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> Â <body> <?php ob_start(); Â Documents/Book of Daniel = $_GET['file']; Â 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); }else { Â Â Â echo "File does not exist"; } Â ob_end_flush(); ?> </body> </html> Â Quote Link to comment Share on other sites More sharing options...
raman Posted November 2, 2008 Share Posted November 2, 2008 Make this correction:Instead of this on line 12, Documents/Book of Daniel = $_GET['file']; Â do like this : $documents = $_GET['file']; This will certainly fix the problem. Quote Link to comment Share on other sites More sharing options...
brdalert Posted November 2, 2008 Author Share Posted November 2, 2008 I am haveing a hard time understanding this can some one fix my script then re post it so it can see how i should look. I'm getting this error now. Parse error: parse error, unex.pected T_IF in /home/e/a/4/10671/10671/public_html/test doenload.php on line 14 Quote Link to comment Share on other sites More sharing options...
raman Posted November 2, 2008 Share Posted November 2, 2008 See just copy and paste this script .It will surely work.The error is too obvious.You are not giving it the right string.$file or $document,use one. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> Â <body> <?php ob_start() Â $file = $_GET['file']; Â 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); }else { Â Â Â echo "File does not exist"; } Â ob_end_flush(); ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
brdalert Posted November 3, 2008 Author Share Posted November 3, 2008 I don't understand where to put my file name within the code. this is my first attempt a php. so could you please put a fake file name in the script where it needs to go so i can see. or give my some more detailed instructions. thank you Quote Link to comment Share on other sites More sharing options...
raman Posted November 3, 2008 Share Posted November 3, 2008 File name: $file='downloadfile"; 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.