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. Link to comment https://forums.phpfreaks.com/topic/131037-force-file-download-and-autostart-on-page-load/ 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(); ?> Link to comment https://forums.phpfreaks.com/topic/131037-force-file-download-and-autostart-on-page-load/#findComment-680332 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. Link to comment https://forums.phpfreaks.com/topic/131037-force-file-download-and-autostart-on-page-load/#findComment-680361 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. Link to comment https://forums.phpfreaks.com/topic/131037-force-file-download-and-autostart-on-page-load/#findComment-680366 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. Link to comment https://forums.phpfreaks.com/topic/131037-force-file-download-and-autostart-on-page-load/#findComment-680368 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> Link to comment https://forums.phpfreaks.com/topic/131037-force-file-download-and-autostart-on-page-load/#findComment-680372 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. Link to comment https://forums.phpfreaks.com/topic/131037-force-file-download-and-autostart-on-page-load/#findComment-680375 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 Link to comment https://forums.phpfreaks.com/topic/131037-force-file-download-and-autostart-on-page-load/#findComment-680376 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> Link to comment https://forums.phpfreaks.com/topic/131037-force-file-download-and-autostart-on-page-load/#findComment-680445 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 Link to comment https://forums.phpfreaks.com/topic/131037-force-file-download-and-autostart-on-page-load/#findComment-680878 Share on other sites More sharing options...
raman Posted November 3, 2008 Share Posted November 3, 2008 File name: $file='downloadfile"; Link to comment https://forums.phpfreaks.com/topic/131037-force-file-download-and-autostart-on-page-load/#findComment-681087 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.