jalaladdin Posted October 12, 2010 Share Posted October 12, 2010 i write this code for book download page that when i click on book download tab ,one empty pdf download for user i want when user ckick on the book download tab one pdf from folder pdf <?phpsession_start();$file = $_GET['file'];header("Content-type: application/pdf");header("Content-Disposition: attachment; filename=". $file);readfile($file);?> <?phpif(isset($_SESSION['username'])){ echo $_SESSION['username']." wellcom"; printf('<form action="logout.php" method="post" class=" dir"> <br/>printf(' <p align="center"><table border="1"> <tbody><tr> <td> <p align="center"><strong>row</strong></p> </td> <td> <p align="center"><strong>bookname</strong></p> </td> <td> <p align="center"><strong>aouther</strong></p> </td> <td><strong>language</strong></td> <td><strong>format</strong></td> <td> <p align="center"><strong>size</strong></p> </td> <td> <p align="center"><strong>downlaod</strong></p> </td> </tr> <td> <div align="center"><font size="2"><font color="#000000" face="Tahoma"><font face="Tahoma"> <div align="center"><font size="2">1</font></font></font></div> </font></font></font></div> </td> <td> <div align="center"><font size="2"><font color="#000000" face="Tahoma"><font face="Tahoma">flash</font></font></font></div> </td> <td> <div align="center"><font size="2">jack</font></div> </td> <td><font size="2"><font size="2"> <div align="center">english</div> </font></font></td> <td><font size="2"><font size="2"> <div align="center">pdf</div> </font></font></td> <td><font size="2"><font size="2"> <div align="center">3.28MB</div> </font></font></td> <td> <div align="center"><font size="2"><strong><a linkindex="158" href="#">Download</a></strong></font></div> </td> </tr> <tr> <td> <div align="center"><font size="2"><font color="#000000" face="Tahoma"><font face="Tahoma"> <div align="center"><font size="2">2</font></font></font></div> </font></font></font></div> </td> <td> <div align="center"><font size="2"><font color="#000000" face="Tahoma"><font face="Tahoma">php</font></font></font></div> </td> <td> <div align="center"><font size="2">bill</font></div> </td> <td><font size="2"><font size="2"> <div align="center">english</div> </font></font></td> <td><font size="2"><font size="2"> <div align="center">pdf</div> </font></font></td> <td><font size="2"><font size="2"> <div align="center">8.28MB</div> </font></font></td> <td> <div align="center"><font size="2"><strong><a linkindex="158" href="#">Download</a></strong></font></div> </td> </tr></tbody></table> </div></table>'); <div align="center"> <input type="submit" value="log out" class="button" /></div></form>'); }else{ echo"please register in the website"; printf('<form action="register.php" method="post" class=" " > </form>');}?> Link to comment Share on other sites More sharing options...
justsomeguy Posted October 12, 2010 Share Posted October 12, 2010 If you're sending a file, that's all you can do. Don't output a bunch of HTML code after the file, because it's going to be counted as data for the file. Use exit after readfile, once the data is sent you need to stop the script. Link to comment Share on other sites More sharing options...
jalaladdin Posted October 12, 2010 Author Share Posted October 12, 2010 If you're sending a file, that's all you can do. Don't output a bunch of HTML code after the file, because it's going to be counted as data for the file. Use exit after readfile, once the data is sent you need to stop the script.what do i do? what can i change this code? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 12, 2010 Share Posted October 12, 2010 Use exit after readfile, once the data is sent you need to stop the script. Link to comment Share on other sites More sharing options...
jalaladdin Posted October 14, 2010 Author Share Posted October 14, 2010 i Use exit after readfile that when i click on book download IE show me file download windowwhat's problem? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 14, 2010 Share Posted October 14, 2010 It doesn't sound like there's a problem, it sounds like that's what you wanted it to do. If you want it to do something other than prompt the user to download the file, then say what you want it to do. Link to comment Share on other sites More sharing options...
jalaladdin Posted October 16, 2010 Author Share Posted October 16, 2010 i wrote this code for book download my website IE show me error <?php if($_SESSION['username']){ echo $_SESSION['username']." welcome"; echo('<form action="logout.php" method="post" class=" dir"> <br/> <div align="center"> <input type="submit" value="log out" class="button" /></div></form>'); $con=mysql_pconnect("localhost","root","");mysql_select_db("register",$con); $result=mysql_query("select 'file_name' from `pdf` where `file_id`='".$_GET['id']."'")or die(mysql_error()); if(mysql_num_rows($result)>0){ $row=mysql_fetch_assoc($result); $file="pdf/".$row['file_name']; $type="application/pdf"; $data=file_get_contents($file); $size=strlen($data); header("content-disposition:attachment; filename=$file"); header("content-type:$type"); header("content-length:$size"); echo $data; }else echo "file doesn't have"; }else echo(' <br/> <div align="center" class=" bg-text1" > please login </div> <br/> <a href="register.html" ><div align="center" class="">register </a></div> '); ?> pdf/file_name.application/pdfWarning: file_get_contents(pdf/file_name) [function.file-get-contents]: failed to open stream: No such file or directory in C:\wamp\www\4\download.php on line 930Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\4\download.php:19) in C:\wamp\www\4\download.php on line 96Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\4\download.php:19) in C:\wamp\www\4\download.php on line 97Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\4\download.php:19) in C:\wamp\www\4\download.php on line 98 Link to comment Share on other sites More sharing options...
boen_robot Posted October 16, 2010 Share Posted October 16, 2010 In your query, you have mysql_query("select 'file_name' from `pdf` where `file_id`='".$_GET['id']."'")or die(mysql_error()); Note the quotes around "file_name". That's a literal string. You have to either not use quotes, or use backticks instead, as you do for `pdf`. Also, as a side note, you might want to cast the GET value, just in case: mysql_query("select `file_name` from `pdf` where `file_id`='". (int) $_GET['id']."'")or die(mysql_error()); Link to comment Share on other sites More sharing options...
jalaladdin Posted October 17, 2010 Author Share Posted October 17, 2010 sorry IE show me these errorWarning: file_get_contents(pdf/file_name) [function.file-get-contents]: failed to open stream: No such file or directory in C:\wamp\www\4\download.php on line 82Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\4\download.php:19) in C:\wamp\www\4\download.php on line 84Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\4\download.php:19) in C:\wamp\www\4\download.php on line 85Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\4\download.php:19) in C:\wamp\www\4\download.php on line 86 Link to comment Share on other sites More sharing options...
Synook Posted October 17, 2010 Share Posted October 17, 2010 You don't seem to have applied boen_robot's changes... what does your code look like now? Link to comment Share on other sites More sharing options...
jalaladdin Posted October 19, 2010 Author Share Posted October 19, 2010 You don't seem to have applied boen_robot's changes... what does your code look like now?please help me what i write code php for download book Link to comment Share on other sites More sharing options...
Synook Posted October 19, 2010 Share Posted October 19, 2010 You use the code you have, and change the line indicated in boen_robot's post to what he suggested. Link to comment Share on other sites More sharing options...
jalaladdin Posted October 19, 2010 Author Share Posted October 19, 2010 i don't know what's problem and IE show me this error again Warning: file_get_contents(pdf/file_name) [function.file-get-contents]: failed to open stream: No such file or directory in C:\wamp\www\4\download.php on line 95Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\4\download.php:19) in C:\wamp\www\4\download.php on line 97Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\4\download.php:19) in C:\wamp\www\4\download.php on line 98Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\4\download.php:19) in C:\wamp\www\4\download.php on line 99 Link to comment Share on other sites More sharing options...
justsomeguy Posted October 19, 2010 Share Posted October 19, 2010 The problem is that the file you're trying to open doesn't exist. Link to comment Share on other sites More sharing options...
boen_robot Posted October 19, 2010 Share Posted October 19, 2010 Do you speak English?Bring a friend to help you out if not.We've all told you what's the problem, I've pinpointed you the problematic line, and given you a copy&paste code to replace it with... what more do you want?Reread everything from the very first reply... fully... do not skip a letter. Link to comment Share on other sites More sharing options...
HungryMind Posted October 19, 2010 Share Posted October 19, 2010 Hi!Try This Method... <?php ob_start(); ?><?php// All Your PHP Code;?><?php ob_flush(); ?> But Seriously I Don't Know How It Does Work With This Error :SCan Anyone Guide! How? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 19, 2010 Share Posted October 19, 2010 It doesn't work regardless of whether or not output buffering is used, because it can't find the file in the first place. Output buffering doesn't matter when the file is missing. Buffering the output will not cause the missing file to appear. Link to comment Share on other sites More sharing options...
HungryMind Posted October 19, 2010 Share Posted October 19, 2010 I Was Talking About This One Error My Friend: Sorry If Didn't Mention. Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\4\download.php:19) in C:\wamp\www\4\download.php on line 97 Link to comment Share on other sites More sharing options...
justsomeguy Posted October 19, 2010 Share Posted October 19, 2010 I understand, but that's not the error. That's a side effect of the error. The error is that the file doesn't exist, and since it prints out the error message saying so then the headers issue those warnings. So the problem isn't with output buffering or with the headers, it's the error message caused by the file not being found. If the file was found and did not issue an error then the other errors also would not happen. Link to comment Share on other sites More sharing options...
HungryMind Posted October 19, 2010 Share Posted October 19, 2010 Hi!Click Here For DEMOindex.php <?phpsession_start();$_SESSION["username"] = "HungryMind";if($_SESSION['username']){ echo $_SESSION['username'] . " welcome"; ?> <form action="logout.php" method="post" class="dir"> <br/> <div align="center"> <input type="submit" value="log out" class="button" /> </div> </form> <?php $con=mysql_pconnect("localhost","root",""); mysql_select_db("register",$con); $result = mysql_query("select file_name, file_id from pdf") or die(mysql_error()); while($row = mysql_fetch_array($result)) { print "<a href=download.php?id=" . $row["file_id"] . ">" . $row["file_name"] . "</a>" . "<br />"; } print "<a href=download.php?id=4>d.pdf</a> I Just Set InValid ID For your Testing." . "<br />";}else{ ?> <br/> <div align="center" class=" bg-text1" >please login</div> <br/> <div align="center" class=""> <a href="register.html">register</a> </div> <?php}?> download.php <?php// I Just Merge This SCRIPT To This Site: http://www.finalwebsites.com/forums/topic/php-file-download// So Thank That Person Who Submitted This Script There :)if (isset($_GET["id"])){ if ($_GET["id"] != "") { $con=mysql_pconnect("localhost","root",""); mysql_select_db("register",$con); $result = mysql_query("select file_name from pdf where file_id=" . $_GET["id"]) or die(mysql_error()); $row = mysql_fetch_array($result); if(mysql_num_rows($result)>0){ $fullPath = $row["file_name"]; //$_GET['download_file']; if ($fd = fopen ($fullPath, "r")) { $fsize = filesize($fullPath); $path_parts = pathinfo($fullPath); $ext = strtolower($path_parts["extension"]); switch ($ext) { case "pdf": header("Content-type: application/pdf"); // add here more headers for diff. extensions header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download break; default; header("Content-type: application/octet-stream"); header("Content-Disposition: filename=\"".$path_parts["basename"]."\""); } header("Content-length: $fsize"); header("Cache-control: private"); //use this to open files directly while(!feof($fd)) { $buffer = fread($fd, 2048); echo $buffer; } } fclose ($fd); exit; } else { echo "File id not found"; } } else { echo "Please set file id."; } }else{ echo "Please set file id.";}?> Link to comment Share on other sites More sharing options...
jalaladdin Posted October 20, 2010 Author Share Posted October 20, 2010 Hi!Click Here For DEMOindex.php<?phpsession_start();$_SESSION["username"] = "HungryMind";if($_SESSION['username']){ echo $_SESSION['username'] . " welcome"; ?> <form action="logout.php" method="post" class="dir"> <br/> <div align="center"> <input type="submit" value="log out" class="button" /> </div> </form> <?php $con=mysql_pconnect("localhost","root",""); mysql_select_db("register",$con); $result = mysql_query("select file_name, file_id from pdf") or die(mysql_error()); while($row = mysql_fetch_array($result)) { print "<a href=download.php?id=" . $row["file_id"] . ">" . $row["file_name"] . "</a>" . "<br />"; } print "<a href=download.php?id=4>d.pdf</a> I Just Set InValid ID For your Testing." . "<br />";}else{ ?> <br/> <div align="center" class=" bg-text1" >please login</div> <br/> <div align="center" class=""> <a href="register.html">register</a> </div> <?php}?> download.php <?php// I Just Merge This SCRIPT To This Site: http://www.finalwebsites.com/forums/topic/php-file-download// So Thank That Person Who Submitted This Script There :)if (isset($_GET["id"])){ if ($_GET["id"] != "") { $con=mysql_pconnect("localhost","root",""); mysql_select_db("register",$con); $result = mysql_query("select file_name from pdf where file_id=" . $_GET["id"]) or die(mysql_error()); $row = mysql_fetch_array($result); if(mysql_num_rows($result)>0){ $fullPath = $row["file_name"]; //$_GET['download_file']; if ($fd = fopen ($fullPath, "r")) { $fsize = filesize($fullPath); $path_parts = pathinfo($fullPath); $ext = strtolower($path_parts["extension"]); switch ($ext) { case "pdf": header("Content-type: application/pdf"); // add here more headers for diff. extensions header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download break; default; header("Content-type: application/octet-stream"); header("Content-Disposition: filename=\"".$path_parts["basename"]."\""); } header("Content-length: $fsize"); header("Cache-control: private"); //use this to open files directly while(!feof($fd)) { $buffer = fread($fd, 2048); echo $buffer; } } fclose ($fd); exit; } else { echo "File id not found"; } } else { echo "Please set file id."; } }else{ echo "Please set file id.";}?> hi Hungrymind thanks a lot i create new folder and new folder call Pdf and i get php.pdf in pdf's folder but IE show me this errorWarning: fopen(php.pdf) [function.fopen]: failed to open stream: No such file or directory in C:\wamp\www\php\download.php on line 23Warning: fclose(): supplied argument is not a valid stream resource in C:\wamp\www\php\download.php on line 51 Link to comment Share on other sites More sharing options...
HungryMind Posted October 20, 2010 Share Posted October 20, 2010 Ok Listen!If U r Testing On Local Server...Then Use FULL Path Of Your PDF File...Like:$fullPath = "C:/wamp/www/php/" . $row["file_name"];If You PDF Files Placed In PDF Folder Under PHP FolderThen U'll Have To Use This PathLike:$fullPath = "C:/wamp/www/php/pdf/" . $row["file_name"];Or Simply Use Direct File Path For testingLike:$fullPath = "C:/wamp/www/php/pdf/php.pdf";But! Use 100% Axact Path Ok.Coz U Are Using Wrong Path, That's Y U Getting This Error (No such file or directory in C:\wamp\www\php\)I Think Your PDF Files Is Placed In C:\wamp\www\php\pdf\php.pdfSo Try This Pathlike:$fullpath = "C:/wamp/www/php/pdf/php.pdf";Did U Checked My Code On IE?Check My DEMO LINK On IE Then Tell Us If U Still Getting An Error.But When Im Using Your Code On My IE, It's Working Perfect :SWhich Version U R Using Of IE?My Friend I Think There Is Something Wrong With Your IE Or Your Wamp Configuration. Link to comment Share on other sites More sharing options...
jalaladdin Posted October 20, 2010 Author Share Posted October 20, 2010 i Have To Use This PathLike:$fullpath = "C:/wamp/www/php/pdf/php.pdf";IE show this error messageWarning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\php\download.php:4) in C:\wamp\www\php\download.php on line 32Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\php\download.php:4) in C:\wamp\www\php\download.php on line 33Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\php\download.php:4) in C:\wamp\www\php\download.php on line 41Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\php\download.php:4) in C:\wamp\www\php\download.php on line 42 Link to comment Share on other sites More sharing options...
HungryMind Posted October 20, 2010 Share Posted October 20, 2010 Hi!Now Ur Path Is Fine...Ok Use These Code For This Error... <?php ob_start(); // AT TOP OF YOUR PAGE ?><?php// USE ALL YOUR PAGE CODE?><?php ob_flush(); // AT BOTTOM OF YOUR PAGE ?> Link to comment Share on other sites More sharing options...
justsomeguy Posted October 20, 2010 Share Posted October 20, 2010 I think output buffering is still the wrong thing to do. The problem in this case is that this is a file being sent. If there is data being outputted, that is going to be considered file data. If that's not part of the actual file data then it's going to corrupt the file. According to the error message the output that is causing the headers to be sent is on line 4 of the download script, what is that line? Can you post the top of the script you're actually using? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.