Retrieving Binary Data from a database "Build Your Own Database Driven Website"

Hi,

Yet another noobie here! Ive dipped into Kevin Yanks “Build Your Own Database Driven Website Using PHP & MySQL” and am finding very helpful.

Trouble is, you come across a problem, and dont know how to fix it.

Working through the Binary Data chapter so I can allow user to upload images to a site and have them shown on the site was all going fine untill I tested it. I had to change the following:

$link = mysqli_connect(‘localhost’, ‘root’, ‘password’);

line in the db.inc.php to include the server name, path to the db and the password for my db file in order to get a connection and get the application to upload a file into the db. The trouble is that the file doesn’t appear in a list under the upload button like it’s supposed to. ‘No file chosen’ appears next to the choose file button, I dont know if that is part of this aswell or whether that expected.

The files are deffinately in the db, I’ve checked.

Any ideas and simply explained solutions would be great.

cheers

It sounds like you’re getting confused here with the browser input field and then actual file you’ve uploaded to the database. The browser input field, which generally shows a text box and an upload button, will not show the files you’ve uploaded in the past. You’ll need to build another page or system to pull files from the database and display them to the user.

I don’t have the book in front of me but I would suspect that if you keep reading it will explain a simple way to pull those files from the database and display to the user.

Hi Jeff, Thanks for your reply, I think that facility is worked into the solution in the book, I’ve attached a screenshot which matches up with the book, minus obviously the links to the images in the database and the buttons that accompany them!

I will carrry on trying to see anywhere where I’ve missed something obvious but any ideas would be great!

Hi,

I’ve been working through Kevin Yanks book “Build Your Own Database Driven Website”.

On the Retrieving Binary Information from the Database chapter, the example of a user being able to upload images to the db and then see a list of these images aswell as view and delete the image files, I have the example working fine when running through a localhost testing server. The problem is when I upload the files to hosts server and try to run the script in a live situation it spots working. I had to change the following:

$link = mysqli_connect(‘localhost’, ‘root’, ‘password’);

line in the db.inc.php to include the server name, path to the db and the password for my db file in order to get a connection and get the application to upload a file into the db. The files upload fine, (and are in the database, I’ve seen them!) but the page isn’t updated with a list of the files stored in the db. The list appears empty, as in the screenshot. Do I need to update this above line again, somewhere else in the scripts in order for the list of files in the database to be listed on the page in the browser.

Any other ideas would be greatly appreciated.

[COLOR=#008000]Two threads merged. Please don’t start a new thread just to add new info about a problem, just post the additional information in the thread.

[/COLOR]

No problem, sorry, I am just desperate to try and get this sorted.

Without seeing the code, this would be the blind leading the blind. They dont give us free copies of the books to reference what you’re asking about.

Hi,

Right, here goes!

Screenshot one shows the page before you upload anything, screenshot three shows how the browser should look like after you’ve uploaded something to the db. Screenshot two shows what mine looks like! The files are deffinately in the db, I’ve looked! I’m just not getting a list.

The scripts work fine when running on a localhost testing server but when I upload it to where the site is hosted and the change the mysqli_connect, red line below for the right details, the images are uploaded to the db fine, but dont get listed on the page.

I think it must be something else I need to adjust when moving the scripts to the webserver as it works fine locally. Strange thing is that it’s connected to the db fine as it is able to upload files to it, it just doesn’t list them anymore.

Any ideas would be great. Please let me know if there is any other info that would help!

index.php

<?php
include_once $_SERVER[‘DOCUMENT_ROOT’] .
‘/includes/magicquotes.inc.php’;

if (isset($_POST[‘action’]) and $_POST[‘action’] == ‘upload’)
{
// Bail out if the file isn’t really an upload
if (!is_uploaded_file($_FILES[‘upload’][‘tmp_name’]))
{
$error = ‘There was no file uploaded!’;
include $_SERVER[‘DOCUMENT_ROOT’] . ‘/includes/error.html.php’;
exit();
}
$uploadfile = $_FILES[‘upload’][‘tmp_name’];
$uploadname = $_FILES[‘upload’][‘name’];
$uploadtype = $_FILES[‘upload’][‘type’];
$uploaddesc = $_POST[‘desc’];
$uploaddata = file_get_contents($uploadfile);

include 'db.inc.php';

// Prepare user-submitted values for safe database insert
$uploadname = mysqli_real_escape_string($link, $uploadname);
$uploadtype = mysqli_real_escape_string($link, $uploadtype);
$uploaddesc = mysqli_real_escape_string($link, $uploaddesc);
$uploaddata = mysqli_real_escape_string($link, $uploaddata);

$sql = "INSERT INTO filestore SET
		filename = '$uploadname',
		mimetype = '$uploadtype',
		description = '$uploaddesc',
		filedata = '$uploaddata'";
if (!mysqli_query($link, $sql))
{
	$error = 'Database error storing file!';
	include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
	exit();
}

header('Location: .');
exit();

}

if (isset($_GET[‘action’]) and
($_GET[‘action’] == ‘view’ or $_GET[‘action’] == ‘download’) and
isset($_GET[‘id’]))
{
include ‘db.inc.php’;

$id = mysqli_real_escape_string($link, $_GET['id']);

$sql = "SELECT filename, mimetype, filedata
		FROM filestore
		WHERE id = '$id'";
$result = mysqli_query($link, $sql);
if (!$result)
{
	$error = 'Database error fetching requested file.';
	include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
	exit();
}

$file = mysqli_fetch_array($result);
if (!$file)
{
	$error = 'File with specified ID not found in the database!';
	include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
	exit();
}

$filename = $file['filename'];
$mimetype = $file['mimetype'];
$filedata = $file['filedata'];
$disposition = 'inline';

if ($_GET['action'] == 'download')
{
	$mimetype = 'application/octet-stream';
	$disposition = 'attachment';
}

// Content-type must come before Content-disposition
header("Content-type: $mimetype");
header("Content-disposition: $disposition; filename=$filename");
header('Content-length: ' . strlen($filedata));

echo $filedata;
exit();

}

if (isset($_POST[‘action’]) and $_POST[‘action’] == ‘delete’ and
isset($_POST[‘id’]))
{
include ‘db.inc.php’;

$id = mysqli_real_escape_string($link, $_POST['id']);

$sql = "DELETE FROM filestore
		WHERE id = '$id'";
if (!mysqli_query($link, $sql))
{
	$error = 'Database error deleting requested file.';
	include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
	exit();
}

header('Location: .');
exit();

}

include ‘db.inc.php’;

$sql = ‘SELECT id, filename, mimetype, description
FROM filestore’;
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = ‘Database error fetching stored files.’;
include $_SERVER[‘DOCUMENT_ROOT’] . ‘/includes/error.html.php’;
exit();
}

$files = array();
while ($row = mysqli_fetch_array($result))
{
$files = array(
‘id’ => $row[‘id’],
‘filename’ => $row[‘filename’],
‘mimetype’ => $row[‘mimetype’],
‘description’ => $row[‘description’]);
}

include ‘files.html.php’;
?>

db.inc.php

<?php
$link = mysqli_connect(‘localhost’, ‘root’, ‘password’);
if (!$link)
{
$error = ‘Unable to connect to the database server.’;
include ‘error.html.php’;
exit();
}

if (!mysqli_set_charset($link, ‘utf8’))
{
$output = ‘Unable to set database connection encoding.’;
include ‘output.html.php’;
exit();
}

if (!mysqli_select_db($link, ‘filestore’))
{
$error = ‘Unable to locate the filestore database.’;
include ‘error.html.php’;
exit();
}
?>

files.html.php

<?php include_once $_SERVER[‘DOCUMENT_ROOT’] .
‘/includes/helpers.inc.php’; ?>
<!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 Repository</title>
<meta http-equiv=“Content-Type”
content=“text/html; charset=utf-8” />
</head>
<body>
<h1>PHP/MySQL File Repository</h1>

	&lt;form action="" method="post" enctype="multipart/form-data"&gt;
		&lt;div&gt;
			&lt;label for="upload"&gt;Upload File:
			&lt;input type="file" id="upload" name="upload"/&gt;&lt;/label&gt;
		&lt;/div&gt;
		&lt;div&gt;
			&lt;label for="desc"&gt;File Description:
			&lt;input type="text" id="desc" name="desc"
					maxlength="255"/&gt;&lt;/label&gt;
		&lt;/div&gt;
		&lt;div&gt;
			&lt;input type="hidden" name="action" value="upload"/&gt;
			&lt;input type="submit" value="Upload"/&gt;
		&lt;/div&gt;
	&lt;/form&gt;

	&lt;?php if (count($files) &gt; 0): ?&gt;

	&lt;p&gt;The following files are stored in the database:&lt;/p&gt;

	&lt;table&gt;
		&lt;thead&gt;
			&lt;tr&gt;
				&lt;th&gt;File name&lt;/th&gt;
				&lt;th&gt;Type&lt;/th&gt;
				&lt;th&gt;Description&lt;/th&gt;
			&lt;/tr&gt;
		&lt;/thead&gt;
		&lt;tbody&gt;
			&lt;?php foreach($files as $f): ?&gt;
			&lt;tr valign="top"&gt;
				&lt;td&gt;
					&lt;a href="?action=view&amp;id=&lt;?php htmlout($f['id']); ?&gt;"
							&gt;&lt;?php htmlout($f['filename']); ?&gt;&lt;/a&gt;
				&lt;/td&gt;
				&lt;td&gt;&lt;?php htmlout($f['mimetype']); ?&gt;&lt;/td&gt;
				&lt;td&gt;&lt;?php htmlout($f['description']); ?&gt;&lt;/td&gt;
				&lt;td&gt;
					&lt;form action="" method="get"&gt;
						&lt;div&gt;
							&lt;input type="hidden" name="action" value="download"/&gt;
							&lt;input type="hidden" name="id" value="&lt;?php htmlout($f['id']); ?&gt;"/&gt;
							&lt;input type="submit" value="Download"/&gt;
						&lt;/div&gt;
					&lt;/form&gt;
				&lt;/td&gt;
				&lt;td&gt;
					&lt;form action="" method="post"&gt;
						&lt;div&gt;
							&lt;input type="hidden" name="action" value="delete"/&gt;
							&lt;input type="hidden" name="id" value="&lt;?php htmlout($f['id']); ?&gt;"/&gt;
							&lt;input type="submit" value="Delete"/&gt;
						&lt;/div&gt;
					&lt;/form&gt;
				&lt;/td&gt;
			&lt;/tr&gt;
			&lt;?php endforeach; ?&gt;
		&lt;/tbody&gt;
	&lt;/table&gt;

	&lt;?php endif; ?&gt;
&lt;/body&gt;

</html>

I have found the reason this doesn’t work is down to the fact that it is running on a ‘Zeus Server’.

Would anyone happen to know any changes I need to make to the script to get it to work!