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>
<form action="" 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>
<?php if (count($files) > 0): ?>
<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 foreach($files as $f): ?>
<tr valign="top">
<td>
<a href="?action=view&id=<?php htmlout($f['id']); ?>"
><?php htmlout($f['filename']); ?></a>
</td>
<td><?php htmlout($f['mimetype']); ?></td>
<td><?php htmlout($f['description']); ?></td>
<td>
<form action="" method="get">
<div>
<input type="hidden" name="action" value="download"/>
<input type="hidden" name="id" value="<?php htmlout($f['id']); ?>"/>
<input type="submit" value="Download"/>
</div>
</form>
</td>
<td>
<form action="" method="post">
<div>
<input type="hidden" name="action" value="delete"/>
<input type="hidden" name="id" value="<?php htmlout($f['id']); ?>"/>
<input type="submit" value="Delete"/>
</div>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</body>
</html>