Hi guys,
I really need some help. I am storing images in my MySQL database (I hate to do it, but I do not have write access to this folder to upload an image). I can load the image in the database and download it again, but it won't display. If I try to display in the page, it is garbage or the red x.
Here is my upload form:
Code: Select all
<?php # upload.php
//This file is the site's file upload page.
ob_start();
session_start();
require_once ('header.php');
if (isset($_POST['filesubmitted']) && isset($_FILES['userfile']) && isset($_SESSION['userid'])) {
$userid = $_SESSION['userid'];
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$image = fread($fp, filesize($tmpName));
if(!get_magic_quotes_gpc()) {$image = addslashes($image);}
fclose($fp);
if(!get_magic_quotes_gpc()) {$fileName = addslashes($fileName);}
global $dbc;
require_once (MYSQL);
$s = "INSERT INTO files (userid, image, name, type, size) VALUES ('$userid', '$image', '$fileName', '$fileType', '$fileSize')";
$t = mysqli_query ($dbc, $s) or trigger_error("Query: $s\n<br />MySQL Error: " . mysqli_error($dbc));
if (mysqli_affected_rows($dbc) == 1) {
echo '<script>alert("File Submitted")</script>';
} else {
echo '<script>alert("File Not Submitted")</script>';
}
}
echo '
<fieldset><legend><span style="color:blue">Form Upload
</span></legend>
<center><p>In this section, you may upload scanned images of the required forms for this position.</p>
<form method="post" enctype="multipart/form-data">
<b>Select file to upload:</b><br />
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input type="file" name="userfile" style="width:50%;" /><br />
<input type="submit" name="submit" value="Upload" style="width:50%;" />
<input type="hidden" name="filesubmitted" value="TRUE" />
</form>
</center>
</fieldset>
';
?>
<?php require_once ('footer.php'); ?>Code: Select all
<?php # view.php
//This file is the site's file upload page.
ob_start();
session_start();
require_once ('header.php');
if (isset($_POST['viewfilesubmitted'])) {
$fileid = $_POST['fileid'];
global $dbc;
require_once (MYSQL);
$s = "SELECT * FROM files WHERE fileid='$fileid'";
$t = mysqli_query ($dbc, $s) or trigger_error("Query: $s\n<br />MySQL Error: " . mysqli_error($dbc));
if (mysqli_affected_rows($dbc) == 1) {
$u = mysqli_fetch_array ($t, MYSQLI_ASSOC);
$size = $u['size'];
$type = $u['type'];
$name = $u['name'];
$image = $u['image'];
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $image;
exit;
} else {
echo 'Error: File could not be retrieved.';
}
}
?>
<?php require_once ('footer.php'); ?>Thanks,
Chris
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: