Dear Anyone,
I am in need of serious help here. I created two tables in a database one to store information about a compmany and its logo while the other stores images. Both of which are in a database. Here are the tables:
CREATE TABLE user (
userID MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
fname VARCHAR(20) NOT NULL,
lname VARCHAR(20) NOT NULL,
email VARCHAR(80) NOT NULL,
category VARCHAR(40) NOT NULL,
membership VARCHAR(20) NOT NULL,
location VARCHAR(80) NOT NULL,
username VARCHAR(30) NOT NULL,
password CHAR(40) NOT NULL,
reg_date CURDATE() NOT NULL,
PRIMARY KEY (userID),
UNIQUE(username),
INDEX(password, username),
UNIQUE(email)
) ENGINE=INNODB;
CREATE TABLE logo (
userID MEDIUMINT UNSIGNED NOT NULL,
logoID INT NOT NULL AUTO_INCREMENT,
filename VARCHAR(255) NOT NULL,
mimetype VARCHAR(50) NOT NULL,
filedata MEDIUMBLOB,
brandIdentity LONGTEXT NOT NULL,
history LONGTEXT NOT NULL,
PRIMARY KEY (logoID),
FOREIGN KEY (userID) REFERENCES user(userID)
) ENGINE=INNODB;
CREATE TABLE design (
userID MEDIUMINT UNSIGNED NOT NULL,
dID INT NOT NULL AUTO_INCREMENT,
cat VARCHAR(40) NOT NULL,
filename VARCHAR(255) NOT NULL,
mimetype VARCHAR(50) NOT NULL,
description VARCHAR(255) NOT NULL,
filedata MEDIUMBLOB,
PRIMARY KEY (dID),
FOREIGN KEY (userID) REFERENCES user(userID)
) ENGINE=INNODB;
I have some scripts that all people to login and upload into the last 2 tables. which are:
loggedin.php
<?php
session_start(); // Start the session.
// If no session value is present, redirect the user:
ob_start();
if (!isset($_SESSION['userID'])) {
require_once ('loginFunction.inc.php');
$url = absolute_url();
header("Location: ".$url);
exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome to ADesigns: Home</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table id="outerTable">
<tr><td id="logo">
<div>
<img src="./images/logo.png" width="150" height="55" alt="company_logo" />
</div>
</td></tr>
<tr><td id ="nav">
<div>
<a href="home.html">Home</a> |
<a href="participate.html">Participate</a> |
<a href="aboutUs.html">About ADesigns</a> |
<a href="contact.html">Contact Us</a>
</div>
</td></tr>
<tr><td id="banner"> <! -- page banner image -->
<img src="images/home.jpg" width="770" height="" alt="welcome to ADesigns">
</td></tr>
<tr><td>
<table id="contentTable">
<tr>
<td id="topLeft">
<br />
<div id="welcomeBox">
<h1>Advertisments</h1>
<p>This section covers adverts from other companies</p>
</div> <! -- mainContent -->
</td>
<td id="topRight">
<br />
<div id="loginRegBox">
<?php
// Print a customized message:
echo "<h1>Logged In!</h1> <p>You are now logged in, {$_SESSION['fname']}!</p>";
?>
<p><b>Congratualtion</b> on your successful login to our website. Now you can start the
fun part and upload your designs. All you have to do is fill up the blanks appropriately.</p>
<p><b>Note:</b> For each upload, you will need to specify that it is a
logo for your design, a picture of yourself or one of your designs.</p>
<p>For ease of use you need to upload one file each for the following:</p>
<p><b>Photograph:</b> a small picture of yourself for visitors to see. Only need once.</p>
<p><b>Logo:</b> your company logo. Also only need one.</p>
<form method="post" action="loggedin_v2.php" enctype="multipart/form-data" >
<!--<div>
<label for="uploadPhoto">Upload Photograph:</label>
<input type="file" name="uploadPhoto" id="upload"/>
</div> -->
<div>
<label for="uploadLogo">Upload Logo:</label>
<input type="file" name="uploadLogo" id="upload"/>
</div>
<!--
I do not need you at the moment:
<div>
<label for="imgType">Image Type:</label>
<input type="radio" name="imageType" id="logo" value="logo" />Logo
<input type="radio" name="imageType" id="photo" value="photo" />Photo
<input type="radio" name="imageType" id="design" value="design" />Design
</div>
-->
<div>
<label for="brandID">Brand Identity:</label>
<textarea name="brandID" rows="4" cols="40" id="brandID"></textarea>
</div>
<div>
<label for="history">Please tell us about yourself:</label>
<textarea name="history" rows="20" cols="40" id="history"></textarea>
</div>
<div>
<input type="submit" name="btnSubmit" id="btnSubmit" value="Upload" />
</div>
</form>
<p><a href="logout.php">Logout</a></p>
</div> <! -- mainContentRight -->
</tr>
</table>
</td></tr>
<tr><td id="footer"><div id="footerLastRow"><p>© ADesigns <a href="#">Privacy Policy</a> | <a href="#">Terms and Conditions</a></p></div></td></tr>
</table>
</body>
</html>
the one that processes the above from the form posted:
<?php
/*Upon submission of scriipt from loggedin.php
*the form is sent here for process
*/
session_start(); //start session
ob_start(); //prevent header errors
if (!isset($_SESSION['userID']) ) {
require_once ('loginFunction.inc.php');
$url = absolute_url();
header("loaction: " .$url);
exit();
}
if ( isset($_GET['action']) )
{
$action = $_GET['action'];
}
else
{
$action = "";
}
$page_title = 'Welcome to ADesigns: Logged In!';
include ('includes/header.html');
$userID = $_SESSION['userID'];
require_once('mysql_connect.php');
//This processes the logo uploads:
if (isset($_FILES['uploadLogo']) )
{
$brandID =trim($_POST['brandID']);
$history =trim($_POST['history']);
/* I want to do a check to see if logo akready exists in the database
* If it does, I want to redirect the designer to the page where you can
* edit the logo table and it is at this same place that you will be allowed
* to upload your designs over and over again.
*/
// Bail out if the file isn't really an upload.
if (!is_uploaded_file($_FILES['uploadLogo']['tmp_name']))
{
exit('There was no file uploaded!');
}
$uploadfile = $_FILES['uploadLogo']['tmp_name'];
$uploadname = $_FILES['uploadLogo']['name'];
$uploadtype = $_FILES['uploadLogo']['type'];
$uploadBrandID = $_POST['brandID'];
$uploadHistory = $_POST['history'];
// Open file or binary reading ('rb')
$tempfile = fopen($uploadfile, 'rb');
// Read the entire file into memory using PHP's
// filesize fucntion to get the size.
$filedata = fread($tempfile, filesize($uploadfile));
// Prepare for database insert by adding backslashes
// before special characters.
$filedata = addslashes($filedata);
//If all goes according to plan:
// Create the SQL query.
$sql = "INSERT INTO logo SET
userID = '$userID',
filename = '$uploadname',
mimetype = '$uploadtype',
filedata = '$filedata',
brandIdentity = '$uploadBrandID',
history = '$uploadHistory'";
// Perform the insert.
$ok = @mysql_query($sql);
if(!$ok)
{
exit('Database error storing file: ' . mysql_error());
}
header('location: ' . $_SERVER['PHP_SELF']);
exit();
}
// This only processes the designs uploadd:
if (isset($_FILES['uploadDesign']) )
{
// Bail out if the file isn't really an upload.
if (!is_uploaded_file($_FILES['uploadDesign']['tmp_name']))
{
exit('There was no file uploaded!');
}
$uploadfile = $_FILES['uploadDesign']['tmp_name'];
$uploadname = $_FILES['uploadDesign']['name'];
$uploadtype = $_FILES['uploadDesign']['type'];
// Open file or binary reading ('rb')
$tempfile = fopen($uploadfile, 'rb');
// Read the entire file into memory using PHP's
// filesize fucntion to get the size.
$filedata = fread($tempfile, filesize($uploadfile));
// Prepare for database insert by adding backslashes
// before special characters.
$filedata = addslashes($filedata);
//If all goes according to plan:
// Create the SQL query for uploading designs.
$sql = "INSERT INTO design SET
userID = '$userID',
filename = '$uploadname',
mimetype = '$uploadtype',
filedata = '$filedata'";
// Perform the insert.
$ok = @mysql_query($sql);
if(!$ok)
{
exit('Database error storing file: ' . mysql_error());
}
header('location: ' . $_SERVER['PHP_SELF']);
exit();
}
/*This script processes the body.php
*
*There is a problem here:
*/
else if ( ($action == 'view' or $action == 'dnld') and isset($_SESSION['userID'] ) )
{
$id = $_GET['id'];
echo'You are here';
// User is retreving a file
$sql = "SELECT filename, mimetype, filedata FROM logo WHERE logoID = '$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 strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 7') )
{
$mimetype = 'application/x-download';
}
}
header("content-disposition: $disposition; filename=$filename");
header("content-type: $mimetype");
header('content-lenght: ' . strlen($filedata));
echo ($filedata);
exit();
}
/*else if($action == 'del' and isset($_SESSION['userID'] ))
{
$id = $_GET['id'];
// User deleting a file
$sql = "DELETE FROM logo WHERE id = '$id'";
$ok = @mysql_query($sql);
if (!$ok)
{
exit('Database error: ' . mysql_error());
}
header('location: ' . $_SERVER['PHP_SELF']);
exit();
}*/
else if ( ($action == 'viewD' or $action == 'dnldD') and isset($_SESSION['userID'] ) )
{
$id = $_GET['id'];
//echo'You are here';
// User is retreving a file
$sql = "SELECT filename, mimetype, filedata FROM design WHERE dID = '$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 == 'dnldD')
{
$disposition = 'attachment';
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5') or strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 7') )
{
$mimetype = 'application/x-download';
}
}
header("content-disposition: $disposition; filename=$filename");
header("content-type: $mimetype");
header('content-lenght: ' . strlen($filedata));
echo ($filedata);
exit();
}
include('includes/body.php');
?>
<?php
include('includes/footer.html');
?>
The following is supposed to display the details stored in the logo table but I only all expect the actual binary code for the logo image is displayed and not the logo image. Why is this so can anyone show me the right way around this issue? I am in need of serious help. There was something similar in this post:
Thank you.