I wrote a completely self contained script that does this with images, from table creation to final retreival and display. You should be able to use roughly the same thing in your use so I will post it.
PHP Code:
<?php
// Database-specific information:
DEFINE (DB_USER, "{$_SERVER['mysql_user']}"); // Set yours.
DEFINE (DB_PASSWORD, "{$_SERVER['mysql_pass']}"); // Set yours.
DEFINE (DB_HOST, "localhost"); // Set yours.
DEFINE (DB_NAME, "my_database"); // Set yours.
DEFINE (DB_TABLE, "images"); // Set yours.
if(isset($_GET['create'])){
// Establishing a database connection
if(!$dbh=mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)){
echo 'Could not connect to MySQL because: '.mysql_error().'. <br />';
}
if(!mysql_select_db(DB_NAME)){
echo 'Couldn\'t connect to the database "'.DB_NAME.'" because: '.mysql_error().'. <br />';
}
//mysql_query('DROP TABLE '.DB_TABLE);
if(mysql_query('CREATE TABLE '.DB_TABLE.' (
image_id int(15) unsigned NOT NULL AUTO_INCREMENT,
image MEDIUMBLOB,
image_type VARCHAR(10),
width int(5),
height int(5),
image_name VARCHAR(30),
KEY image_id (image_id)
);')){
echo 'Created table: "'.DB_TABLE.'".';
}else{
echo 'Table: "'.DB_TABLE .'" could not be created because: '.mysql_error().'.';
}
print '<br /><br /><a href="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'">Continue</a>';
}elseif(isset($_GET['upload'])){
if(!empty($_FILES['file']['name'])) {
// Check MIME type
$size = getimagesize($_FILES['file']['tmp_name']);
if(!eregi('image/', $size['mime'])){
exit('<b>Image</b> uploads only! Max size: <b>200 kilobytes</b>.');
}
// Connect to MySQL:
$db_connection = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die ('Could not connect to MySQL: ' . mysql_error());
// Select the database:
mysql_select_db (DB_NAME) or die ('Could not select the database: ' . mysql_error());
// Read the uploaded file.
$image = addslashes(fread(fopen($_FILES['file']['tmp_name'], "r"), $_FILES['file']['size']));
list($filename, $ext) = spliti('\.', $_FILES['file']['name']);
list($width, $height) = getimagesize($_FILES['file']['tmp_name']);
// Generate the query.
$query = "INSERT INTO images VALUES (0, '$image', '{$_FILES['file']['type']}', '$width', '$height', '$filename')";
// Execute the query and report on its success.
if (mysql_query ($query)) {
echo 'Image stored. ID: "' . mysql_insert_id() . '"!';
print '<br /><br /><a href="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'">Continue</a>';
} else {
echo 'Image storage error! ' . mysql_error();
}
// Close the database connection.
mysql_close();
}elseif(isset($_POST) and !$_FILES['file']['error'][$i]==0){
exit('<b>Image</b> uploads only! Max size: <b>200 kilobytes</b>.');
}else{
echo <<<END
<form action="http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}?upload" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="200000" />
Select a file to upload: <input type="file" name="file" />
<br />
<input type="submit" name="submit" value="Submit!" />
</form>
END;
}
}elseif(isset($_GET['index'])){
// Connect to MySQL:
$db_connection = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die ('Could not connect to MySQL: ' . mysql_error());
// Select the database:
mysql_select_db (DB_NAME) or die ('Could not select the database: ' . mysql_error());
// Retrieve the image information.
$query = "SELECT image_id, image_name FROM images";
$result=mysql_query($query);
while($row = mysql_fetch_array($result)){
extract($row);
print('<a href="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?page='.$row ['image_id'].'">'.$row['image_name'].'</a><br />');
}
print '<br /><br /><a href="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'">Return to main page!</a>';
}elseif(isset($_GET['display'])){
// Connect to MySQL:
$db_connection = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die ('Could not connect to MySQL: ' . mysql_error());
// Select the database:
mysql_select_db (DB_NAME) or die ('Could not select the database: ' . mysql_error());
// Retrieve the image information.
$query = "SELECT image, image_type FROM images WHERE image_id={$_GET['display']}";
if ($query_result = mysql_query ($query)) {
$image = mysql_fetch_array($query_result);
header ("Content-type: $image[1]");
echo $image[0];
}
// Close the database connection.
mysql_close();
}elseif(isset($_GET['page'])){
// Connect to MySQL:
$db_connection = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die ('Could not connect to MySQL: ' . mysql_error());
// Select the database:
mysql_select_db (DB_NAME) or die ('Could not select the database: ' . mysql_error());
// Retrieve the image information.
$query = "SELECT image_name, width, height FROM images WHERE image_id={$_GET['page']}";
$query_result=mysql_query($query) or die (mysql_error());
$row = mysql_fetch_array($query_result) or die (mysql_error());;
print '<img src="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?display='.$_GET['page'].'" width="'.$row['width'].'" height="'.$row['height'].'" alt="'.$row['image_name'].'">';
print "\n\n".'<br /><br /><a href="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?index">Back</a>';
}else{
echo <<<END
Step 1: <a href="http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}?create">Create a MySQL table.</a><br />
Step 2: <a href="http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}?upload">Upload an image.</a><br />
Step 3: <a href="http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}?index">Show the image index</a><br />
END;
}
?>
Bookmarks