Hi Ya’ll, I am working through Kevin’s book on Database Driven websites 3rd edition. In chapter 10, when I write the script “filestore.php” (I couldn’t find it in the code download so I typed it myself - always a potential trouble spot!) the script returns an error:
“Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\File_upload_LSU\fileupload.php:2) in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\File_upload_LSU\fileupload.php on line 56”.
The script does download the file to the database and when I refresh the page it is listed. But if I delete the file I get the same error only the problem is on line 91. Here is the code:
<?php
$dbcnx = @mysql_connect('localhost', 'root', 'print01');
if (!$dbcnx) {
exit('<p>Unable to connect to the' . 'database server at this time.</p>');
}
if (!@mysql_select_db('ijdb')) {
exit('<p>Unable to locate the joke' . 'database at this time.</p>');
}
if (isset($_GET['action'])) {
$action = $_GET['action'];
}else{
$action = '';
}
if (($action == 'view' or $action == 'dnld') and
isset($_GET['id'])) {
$id = $_GET['id'];
//user is retrieving file
$sql = "SELECT filename, mimetype, filedata
FROM filestore WHERE id = '$id'";
$result = @mysql_query($sql);
if (!$result) {
exit('Database error: ' . mysql_error());
}
$file = mysql_fetch_array($result);
if (!$file) {
exit ('File with given ID not found in database!');
}
$filename = $file['filename'];
$mimetype = $file['mimetype'];
$filedata = $file['filedata'];
$disposition = 'inline';
if ($action == 'dnld') {
$disposition = 'attachment';
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5') or
strops($_SERVER['HTTP_USER_AGENT'], 'Opera 7')) {
$mimetype = 'application/x-download';
}
}
header("content-disposition: $disposition; filename=$filename");
header("content-type: $mimetype");
header('content-length: ' . strlen($filedata));
echo $filedata;
exit();
} elseif ($action == 'del' and isset($_GET['id'])) {
$id = $_GET['id'];
//user is deleting a file
$sql = "DELETE FROM filestore WHERE id = '$id'";
$ok = @mysql_query($sql);
if (!$ok) {
exit('Database error: ' . mysql_error());
}
header('location: ' . $_SERVER['PHP_SELF']);
exit();
} elseif (isset($_FILES['upload'])) {
//bail out if the file isn't really an upload
if (!is_uploaded_file($_FILES['upload']['tmp_name'])) {
exit('There was no file uploaded!');
}
$uploadfile = $_FILES['upload']['tmp_name'];
$uploadname = $_FILES['upload']['name'];
$uploadtype = $_FILES['upload']['type'];
$uploaddesc = $_POST['desc'];
//open file for binary reading ('rb')
$tempfile = fopen($uploadfile, 'rb');
//read the entire file into memory using PHP's
//filesize function to get the file size
$filedata = fread($tempfile, filesize($uploadfile));
//prepare for database insert by adding backslashes
//before special characters.
$filedata = addslashes($filedata);
//create the sql query
$sql = "INSERT INTO filestore SET
filename = '$uploadname',
mimetype = '$uploadtype',
description = '$uploaddesc',
filedata = '$filedata'";
//perform the insert
$ok = @mysql_query($sql);
if (!$ok) {
exit('Database error storing file: ' . mysql_error());
}
header('location: ' . $_SERVER['PHP_SELF']);
exit();
}
//default page view: list stored files
$sql = 'SELECT id, filename, mimetype, description
FROM filestore';
$filelist = @mysql_query($sql);
if (!$filelist) {
exit('Database error: ' . mysql_error());
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP/MySQL File Upload for LSU/SVM</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8" />
</head>
<body>
<h1>File Upload for LSU/SVM</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
method="post" enctype="multipart/form-data">
<div>
<label for="upload">Upload File:
<input type="file" id="upload" name="upload"/></label>
</div>
<div>
<label for="desc">File Description:
<input type="text" id="desc" name="desc"
maxlength="255"/></label>
</div>
<div>
<input type="hidden" name="action" value="upload"/>
<input type="submit" value="Upload" />
</div>
</form>
<p>The following files are stored in the database:</p>
<table>
<thead>
<tr>
<th>File name</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
if (mysql_num_rows($filelist) > 0) {
while ($f = mysql_fetch_array($filelist)) {
?>
<tr valign="top">
<td>
<a href="<?php echo $_SERVER['PHP_SELF'];
?>?action=view&id=<?php echo $f['id']; ?>">
<?php echo $f['filename']; ?></a>
</td>
<td><?php echo $f['mimetype']; ?></td>
<td><?php echo $f['description']; ?></td>
<td>
[<a href="<?php echo $_SERVER['PHP_SELF'];
?>?action=dnld&id=<?php echo $f['id']; ?>"
>Download</a> |
<a href="<?php echo $_SERVER['PHP_SELF'];
?>?action=del&id=<?php echo $f['id']; ?>"
onclick="return confirm('Delete this file?');"
>Delete</a>]
</td>
</tr>
<?php
}
} else {
?>
<tr><td colspan="3">No Files!</td></tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>
Thanks much Bob