Headers already sent problem online, but not offline

From: Dariusz (ng_at_lycaus.plusYOURSHIT.com)
Date: 04/12/04


Date: Mon, 12 Apr 2004 14:12:05 GMT

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



Relevant Pages

  • Re: Email Form Scripting Error Help
    ... Warning: session_start: Cannot send session cache limiter - headers already sent (output started at ... Cannot modify header information - headers already sent by (output ... Make sure you put 'session_start;' at the very top of your script. ...
    (comp.lang.php)
  • Re: Email Form Scripting Error Help
    ... the script runs, I get an error page, although the e-mail is sent off. ... Warning: session_start: Cannot send session cache limiter - headers already sent (output started at ... Cannot modify header information - headers already sent by (output ...
    (comp.lang.php)
  • Warning Message
    ... connecting to a secure web site. ... The user accesses a site over the web the ... "Warning. ... Cannot modify header information - header ...
    (microsoft.public.windows.inetexplorer.ie6.browser)
  • Re: [PHP] Problem with Redirect
    ... Warning: Cannot modify header information - headers already sent ... You could also consider not doing a redirect at all, ...
    (php.general)
  • Re: Splitting uploads up/ concatenating them back together
    ... my downloader script looks like this: ... Warning: file_get_contentsexpects parameter 1 to be string, ... Warning: fclose: supplied argument is not a valid stream resource in ... Cannot modify header information - headers already sent by ...
    (comp.lang.php)