Hi,
i have created a File repository to store my .jpg image files in a database called filestore. I can easily store them in my database and can extract the information about the files to be displayed in my browser. Next to each file is a download button, when i try to download the file and try to view it, i get a message from Windows Photo gallery saying:
[B]Photo gallery can’t open this picture or video. The file seems to be damaged or corrupted
[CODE] code snippet
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 filestore db’;
include $_SERVER[‘DOCUMENT_ROOT’] . ‘/includes/error.html.php’;
exit();
}
// Use variables to send the data in http headers
$filename = $file[‘filename’];
$mimetype = $file[‘mimetype’];
$filedata = $file[‘filedata’];
$disposition = ‘inline’; // use to download
if($_GET[‘action’] == ‘download’)
{
$mimetype = ‘application/octet-stream’; // force download in older browsers
$disposition = ‘attachment’;
}
// Content-type must come before Content-disposition
header(“Content-type: $mimetype”);
header(“Content-disposition: $disposition; filename=$filename”); // force the browser to display save dialog for download
header(‘Content-length: ‘ . strlen($filedata) );
echo $filedata;
exit();
}
html form, files.html.php
[CODE]
<?php include_once $_SERVER[‘DOCUMENT_ROOT’] .
‘/includes/helpers.inc.php’; ?>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN”
“http://www.w3.org/TR/html4/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>
<!– multipart/form-data ensures the actual file is sent with the form data –>
<form action=” ” method=”post” enctype=”multipart/form-data”>
<div><label for=”upload”> Select file to upload: <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=”Submit”/>
</div>
</form>
<?php if(count($files) > 0): ?>
<p> The following are files stored in the filestore 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>
what do i need to do to ensure that i can successfully view my image files?