jagguy Posted June 26, 2007 Share Posted June 26, 2007 I have a link to text file which is on my server. When the user clicks on the link the textfile simply displays in a new window. What i want to happen is that the file is able to be downloaded to the users machine. I can upload with php so can i download? How do i set this up? Quote Link to comment Share on other sites More sharing options...
suma237 Posted June 26, 2007 Share Posted June 26, 2007 check this site http://www.php-mysql-tutorial.com/php-mysql-upload.php Quote Link to comment Share on other sites More sharing options...
jagguy Posted June 26, 2007 Author Share Posted June 26, 2007 This seeems to store the whole file in mysql. What if i have a 1mb doc to upload that contains some images? most servers have limits on wha you can store in mysql so a file storage is simpler. Quote Link to comment Share on other sites More sharing options...
redarrow Posted June 26, 2007 Share Posted June 26, 2007 http://www.phpfreaks.com/forums/index.php/topic,95433.0.html Quote Link to comment Share on other sites More sharing options...
jagguy Posted June 27, 2007 Author Share Posted June 27, 2007 is forcing the download the regular of doing this? I dont want to store a whole file in mysql and just wanted to click on alink to download a file. txt files usually appear in a new browser etc. Quote Link to comment Share on other sites More sharing options...
jagguy Posted June 27, 2007 Author Share Posted June 27, 2007 I am testing the download code. When i run this code the file downloads OK but it appends the html code to the file so i get the file and html file it is in? How does this happen? I just want a link to a file then it downloads. <body> <div id="boxlayer1" > <div id="boxlayer2" class="lbox">5eareresae56</div> <div id="boxlayer3" class="lbox">reerertertert</div> <div id="boxlayer4" class="lbox">retererertert</div> </div> <div id="content1" > jlkjkljkljlkjkljklj <?php $file="linuxhelp.txt"; // $dir = "../log/exports/"; if ((isset($file))&&(file_exists($file))) { header("Content-type: application/force-download"); header('Content-Disposition: inline; filename="' .$file . '"'); header("Content-Transfer-Encoding: Binary"); header("Content-length: ".filesize($file)); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . $file . '"'); readfile("$file"); } else { echo "No file selected"; } //end if ?> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
jagguy Posted June 28, 2007 Author Share Posted June 28, 2007 I put the code to download in a seprate file and it downloads without extra html as before. q)So if I called a script to download via a Get(div2.html) then the php file runs (div3.html)and the browser doesn't goto div3.html , so i have no need for a redirect like header( "Location: http://localhost/school/test/div2.html" ); why? q) will this script handle pdf .xls and other file types? div2.html ... <body> <div id="boxlayer1" > <div id="boxlayer2" class="lbox">5eareresae56</div> <div id="boxlayer3" class="lbox">reerertertert</div> <div id="boxlayer4" class="lbox">retererertert</div> </div> <div id="content1" > jlkjkljkljlkjkljklj <?php $file="linuxhelp.txt"; echo "<br> <a href='div3.html?file=".$file."'>file download</a>"; div3.html $file=$_GET['file']; //echo "sss"; if ((isset($file))&&(file_exists($file))) { header("Content-type: application/force-download"); header('Content-Disposition: inline; filename="' .$file . '"'); header("Content-Transfer-Encoding: Binary"); header("Content-length: ".filesize($file)); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . $file . '"'); readfile("$file"); // exit(); } else { echo "No file selected"; } //end if Quote Link to comment Share on other sites More sharing options...
redarrow Posted June 28, 2007 Share Posted June 28, 2007 There you go ok. <?php $filename = $_GET['file']; // required for IE, otherwise Content-disposition is ignored if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); $file_extension = strtolower(substr(strrchr($filename,"."),1)); if( $filename == "" ) { echo "<html><title>eLouai's Download Script</title><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>"; exit; } elseif ( ! file_exists( $filename ) ) { echo "<html><title>eLouai's Download Script</title><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>"; exit; }; switch( $file_extension ) { case "pdf": $ctype="application/pdf"; break; case "exe": $ctype="application/octet-stream"; break; case "zip": $ctype="application/zip"; break; case "doc": $ctype="application/msword"; break; case "xls": $ctype="application/vnd.ms-excel"; break; case "ppt": $ctype="application/vnd.ms-powerpoint"; break; case "gif": $ctype="image/gif"; break; case "png": $ctype="image/png"; break; case "jpeg": case "jpg": $ctype="image/jpg"; break; default: $ctype="application/force-download"; } header("Pragma: public"); // required header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); // required for certain browsers header("Content-Type: $ctype"); // change, added quotes to allow spaces in filenames, by Rajkumar Singh header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($filename)); readfile("$filename"); exit(); ?> Quote Link to comment Share on other sites More sharing options...
jagguy Posted July 1, 2007 Author Share Posted July 1, 2007 Hi, I can force a download file with this code and it works. I know what the switch statement does but I am unsure about the rest. Is all this code needed for a forced download? $file_extension = strtolower(substr(strrchr($file,"."),1)); if ((isset($file))&&(file_exists($file))) { switch( $file_extension ) { case "pdf": $ctype="application/pdf"; break; case "exe": $ctype="application/octet-stream"; break; ... } header("Pragma: public"); // required header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); // required for certain browsers header("Content-type: application/force-download"); header('Content-Disposition: inline; filename="' .$file . '"'); header("Content-Transfer-Encoding: Binary"); header("Content-length: ".filesize($file)); header("Content-Type: $ctype"); header('Content-Disposition: attachment; filename="' . $file . '"'); readfile("$file"); Quote Link to comment Share on other sites More sharing options...
jagguy Posted July 2, 2007 Author Share Posted July 2, 2007 Just a quick problem I am downloading a file from a dir and it works fine if I store the file in the same dir as the php file. I want to download from a f'iles/filename' dir but I can't get it to work from that dir. Also having a / or \ slash for dir as I am using windows(don't ask) but will host it on linux which is better. Thi code fails because of the file to download is not in current dir $file= $_GET['file']; $path= $_GET['path']; $file_path=$file.'/'.$path; $file_extension = strtolower(substr(strrchr($file,"."),1)); if ((isset($file))&&(file_exists($file_path))) { switch( $file_extension ) { case "pdf": $ctype="application/pdf"; break; .. default: $ctype="application/force-download"; } header("Content-type: application/force-download"); header('Content-Disposition: inline; filename="' .$file_path . '"'); header("Content-Transfer-Encoding: Binary"); header("Content-length: ".filesize($file)); header("Content-Type: $ctype"); header('Content-Disposition: attachment; filename="' . $file . '"'); readfile("$file"); } else echo "No file selected"; Quote Link to comment Share on other sites More sharing options...
jagguy Posted July 4, 2007 Author Share Posted July 4, 2007 My downloads fail to work since i moved them into another folder. I am getting tired and cant work it out. The files exists and I get nothing in the downloaded files. They are all txt files . $file= $_GET['file']; $path= $_GET['path']; //$file_path=$path.'/'.$file; $file_path = $_SERVER['DOCUMENT_ROOT'] . '/school/test/files/' . $file; $file_extension = strtolower(substr(strrchr($file,"."),1)); if ((isset($file))&&(file_exists('files/'.$file))) { switch( $file_extension ) { case "pdf": $ctype="application/pdf"; break; case "exe": $ctype="application/octet-stream"; break; case "zip": $ctype="application/zip"; break; case "doc": $ctype="application/msword"; break; case "xls": $ctype="application/vnd.ms-excel"; break; case "ppt": $ctype="application/vnd.ms-powerpoint"; break; case "gif": $ctype="image/gif"; break; case "png": $ctype="image/png"; break; case "jpg": $ctype="image/jpg"; break; default: $ctype="application/force-download"; } header("Content-type: application/force-download"); header('Content-Disposition: inline; filename="' .$file_path . '"'); header("Content-Transfer-Encoding: Binary"); header("Content-length: ".filesize($file)); header("Content-Type: $ctype"); header('Content-Disposition: attachment; filename="' . $file . '"'); readfile("$file"); } else echo "No file selected"; Quote Link to comment Share on other sites More sharing options...
jagguy Posted July 4, 2007 Author Share Posted July 4, 2007 This works ... header("Content-type: application/force-download"); // header('Content-Disposition: inline; filename="' .$file_path . '"'); header("Content-Transfer-Encoding: Binary"); // header("Content-length: ".filesize($file_path)); header("Content-length: ".filesize($file_path)); header("Content-Type: $ctype"); // header('Content-Disposition: attachment; filename="' . $file_path . '"'); header('Content-Disposition: attachment; filename="' . $file . '"'); // readfile("$file_path"); readfile("$file_path"); 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.