By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,971 Members | 1,451 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,971 IT Pros & Developers. It's quick & easy.

Headers already sent problem online, but not offline

P: n/a
I have a problem where when I run the PHP code offline, there are no errors
produced and the code runs as expected. However when I uploaded the same
script and run it, it says the headers have already been sent.

The script is for a "downloader" type script, where the user clicks on a
file link (to the PHP script) using $_GET to pass the selected files ID to
the script, and the information about the file is read out from a database,
also updating a field in the database on how many times a file has been
accessed, and then produces the browsers "save as" dialog box according to
which file type was accessed.

Below is the code, if anyone can help how to sort this problem out. I put
line numbers in to show where the errors occur, along with the error
report.

Thanks

Dariusz
// Connect (log on) to the database
$connection = @mysql_connect("localhost", "xxx", "xxx");

mysql_select_db($DatabaseName);

// Display database entries
$sql = mysql_query("SELECT * FROM $TableName WHERE f_id='$FileID'");

$FileInfo = mysql_fetch_row($sql); <-- Line 22
// Read from the array the files URL
$FileURL = 'http://'.$FileInfo[0];
// Read from the array the files name
$FileName = $FileInfo[1];
// Read file size from the database
// Problem using the built-in function to read file size of stated file
$FileSize = $FileInfo[2];
// Write code for number of accesses a file has had
$FileAccessed = $FileInfo[3];

$FileAccessed = $FileAccessed + 1;

$sql = ("UPDATE $TableName SET f_accessed=$FileAccessed WHERE
f_id='$FileID'");
$result = @mysql_query($sql, $connection);

$FileType = substr($FileID, 0, 3);

MYSQL_CLOSE();

//Screensavers
if ($FileType == 'scr')
{
// We'll be outputting a screensaver
header('Content-type: application/octet-stream'); <-- Line 46
// Filename
header("Content-Disposition: attachment; filename=$FileName;"); <-- Line48
header('Content-Transfer-Encoding: binary'); <-- Line 49
// Calculate file size
header('Content-Length: '.$FileSize); <-- Line 51
// The location of the file
readfile($FileURL); <-- Line 53
}
Errors:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result
resource in /home/php/downloader.php on line 22

Warning: Cannot modify header information - headers already sent by (output
started at /home/php/downloader.php:22) in /home/php/downloader.php on line
46

Warning: Cannot modify header information - headers already sent by (output
started at /home/php/downloader.php:22) in /home/php/downloader.php on line
48

Warning: Cannot modify header information - headers already sent by (output
started at /home/php/downloader.php:22) in /home/php/downloader.php on line
49

Warning: Cannot modify header information - headers already sent by (output
started at /home/php/downloader.php:22) in /home/php/downloader.php on line
51

Warning: readfile(http://): failed to open stream: Success in
/home/php/downloader.php on line 53
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <mw*********************@wards.force9.net>,
ng@lycaus.plusYOURSHIT.com (Dariusz) wrote:
Below is the code, if anyone can help how to sort this problem out. I put
line numbers in to show where the errors occur, along with the error
report.
[...]
Errors:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result
resource in /home/php/downloader.php on line 22

Warning: Cannot modify header information - headers already sent by (output
started at /home/php/downloader.php:22) in /home/php/downloader.php on line
46


The 'headers already sent' error is because of the first error (the
mysql_fetch_row() one on line 22). The headers can't be sent because the
first error message got in the way.

Your mysql_query:

$sql = mysql_query("SELECT * FROM $TableName WHERE f_id='$FileID'");

failed for some reason.

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #2

P: n/a
ng@lycaus.plusYOURSHIT.com (Dariusz) wrote in
news:mw*********************@wards.force9.net:
I have a problem where when I run the PHP code offline, there are no
errors produced and the code runs as expected. However when I
uploaded the same script and run it, it says the headers have already
been sent.
That's a side effect of your primary problem, namely that your SQL query is
failing. That results in an error message when you try to use the result
of the query, and that error message is being output as text, which means
that PHP has to first send out a header and then the error message, which
means that you can't send out any futher headers.
// Connect (log on) to the database
$connection = @mysql_connect("localhost", "xxx", "xxx");

mysql_select_db($DatabaseName);

// Display database entries
$sql = mysql_query("SELECT * FROM $TableName WHERE f_id='$FileID'");
All three preceding statements need error checking; you're just blindly
assuming that they're all going to be successful.

$FileInfo = mysql_fetch_row($sql); <-- Line 22


This will generate an error message if any of the preceding operations
failed.

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.