Thanks very much alti for you respond it was great help for me :))
Uploading files into a MySQL database using PHP
Collapse
X
-
Error on downloading files
Hi, I am a newbie here in bytes forum.
I exactly follow the whole script you post, from phase 0 to phase 4. Fortunately there was no error and the codes works fine from phase 0 to phase 3. I have a problem in downloading. When I hit the download link, an error occur.
Here's the error..
Warning: Cannot modify header information - headers already sent by (output started at c:\Inetpub\wwwr oot\uploadingfi les\ddl.php:10) in c:\Inetpub\wwwr oot\uploadingfi les\ddl.php on line 42
Warning: Cannot modify header information - headers already sent by (output started at c:\Inetpub\wwwr oot\uploadingfi les\ddl.php:10) in c:\Inetpub\wwwr oot\uploadingfi les\ddl.php on line 43
Warning: Cannot modify header information - headers already sent by (output started at c:\Inetpub\wwwr oot\uploadingfi les\ddl.php:10) in c:\Inetpub\wwwr oot\uploadingfi les\ddl.php on line 44
What is seem to be the error. Thanks in advance.Comment
-
Hi roseple.
ririe is right.
The headers are sent as soon as you start sending content to the browser, at which point you can not alter them (obviously).
If you try, you get that error.
Even a single space before your <?php tags will cause this, as will any echo calls from your scripts.
If there is absolutely no way to avoid sending content before altering the headers, you can use the Output Buffering Control functions to delay sending the content.Comment
-
MySQLi is designed to work with MySQL 4.1.3 or higher and gives access to more advanced features in MySQL. It requires PHP 5.0 to use. It also gives you more control because you can change MySQL settings using MySQLi without stopping and starting the MYSQL server.
MySQLi is also the OOP version of MySQL extension. MySQLi supports some things that the old MySQL extension doesn't. A lot of people still use the original MySQL extension vs the new MySQLi extension because MySQLi requires MySQL 4.1.13+ and PHP 5.0.7+. However, they both provide access MySQL features.Comment
-
Oh I see. But like me who is a newbie programmer, I still used the original MySql extension. So in above code you gave, I change all the mysqli to mysql. You can see on my attached file.Code:<!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 # Make sure an ID was passed if(isset($_GET['id'])) { # Get the ID $id = $_GET['id']; # Make sure the ID is in fact a valid ID if(!is_numeric($id) || ($id <= 0)) { die("The ID is invalid!"); } # Connect to the database $dbLink = mysql_connect("localhost", "root", "777") or die (mysql_error()); mysql_select_db("filestorage", $dbLink) or die(mysql_error()); # Fetch the file information $query = " SELECT FileMime, FileName, FileSize, FileData FROM filestorage WHERE FileID = {$id}"; $result = @mysql_query($query,$dbLink) or die("Error! Query failed: <pre>". mysqli_error($dbLink) ."</pre>"); # Make sure the result is valid if(mysql_num_rows($result) == 1) { # Get the row $row = mysql_fetch_assoc($result); # Print headers header("Content-Type:".$row['FileMime']); header("Content-Length:".$row['FileSize']); header("Content-Disposition:attachment;filename=".$row['FileName']); # Print data echo $row['FileData']; } else { echo "Error! No image exists with that ID."; } # Free the mysqli resources @mysql_free_result($result); @mysql_close($dbLink); } else { echo "Error! No ID was passed."; } ?> </body> </html>
Comment
-
Code:<?php # Make sure an ID was passed if(isset($_GET['id'])) { # Get the ID $id = $_GET['id']; # Make sure the ID is in fact a valid ID if(!is_numeric($id) || ($id <= 0)) { die("The ID is invalid!"); } # Connect to the database $dbLink = mysql_connect("localhost", "root", "777") or die (mysql_error()); mysql_select_db("filestorage", $dbLink) or die(mysql_error()); # Fetch the file information $query = " SELECT FileMime, FileName, FileSize, FileData FROM filestorage WHERE FileID = {$id}"; $result = @mysql_query($query,$dbLink) or die("Error! Query failed: <pre>". mysqli_error($dbLink) ."</pre>"); # Make sure the result is valid if(mysql_num_rows($result) == 1) { # Get the row $row = mysql_fetch_assoc($result); # Print headers header("Content-Type:".$row['FileMime']); header("Content-Length:".$row['FileSize']); header("Content-Disposition:attachment;filename=".$row['FileName']); # Print data echo $row['FileData']; } else { echo "Error! No image exists with that ID."; } # Free the mysqli resources @mysql_free_result($result); @mysql_close($dbLink); } else { echo "Error! No ID was passed."; } ?>
Roseple, you just try use the codes above and delete all those thing...
-----delete-----
Code:<!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> </body> </html>
The opening for html, head and all the things are not required for this code.As Atli said the headers are sent as soon as you start sending content to the browser, so you can not alter them. If you put the html codes, the program will assume that the header are already sent. So your actually program cannot running properly and you will get errors.Comment
Comment