Image Problem
| From: | Monil Chheda | Date: | Sun, 27 Jun 2004 19:54:15 +0000 |
| Subject: | Image Problem | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-189128@lists.php.net to get a copy of this message | ||
Hi,
I store images in DB properly... no issues using the
following code....
<?php
// store.php3 - by Florian Dittmer <dittmer@gmx.net>
// Example php script to demonstrate the storing of
binary files into
// an sql database. More information can be found at
http://www.phpbuilder.com/
?>
<html>
<head><title>Store binary data into SQL
Database</title></head>
<body>
<?php
// code that will be executed if the form has been
submitted:
if ($submit) {
// Include constants for database connectivity
include("constants.php");
// The Database server connection function
$connection =
mysql_connect("$host","$db_user","$db_pass") or
die("Couldn't connect to the MySql Database Server");
// Database selection from the DB server
$db = mysql_select_db("$db_name",$connection) or
die("Couldn't select the required Database.");
$data = addslashes(fread(fopen($banner_image,
"r"), filesize($banner_image)));
$result = MYSQL_QUERY("INSERT INTO banner_details
(text,banner_image,filename,filesize,filetype) ".
"VALUES
('$banner_description','$data','$form_data_name','$form_data_size','$form_data_type')")
or die("Couldnt insert banner in the Db");
echo "Banner uploaded";
$id= mysql_insert_id();
print "<p>This file has the following Database ID:
<b>$id</b>";
} else {
// else show the form to submit new data:
?>
<form method="post" action="<?php echo $PHP_SELF;
?>" enctype="multipart/form-data">
Banner Text:<br>
<input type="text" name="banner_description"
size="40">
<input type="hidden" name="MAX_FILE_SIZE"
value="1000000">
<br>Image to upload/store in database:<br>
<input type="file" name="banner_image" size="40">
<p><input type="submit" name="submit"
value="submit">
</form>
<?php
}
?>
</body>
</html>
------------
I use the foll file to reterieve it...
<?
/*
Aim: To fetch random image and its corresponding
text from the database.
Working: Generate a random number within the range of
1 and number of rows present in the DB and then fetch
the appropriate ROW ID.
Includes: constants.php for DB connectivity
*/
// Include constants for database connectivity
include("constants.php");
// The Database server connection function
$connection =
mysql_connect("$host","$db_user","$db_pass") or
die("Couldn't connect to the MySql Database Server");
// Database selection from the DB server
$db = mysql_select_db("$db_name",$connection) or
die("Couldn't select the required Database.");
/*
Fetching the number of rows in the DB table
*/
// Reteriving the data from the banners table in the
DB
$sql = "SELECT *" .
"FROM banner_details";
// Executing the Sql select statement
$result = mysql_query($sql, $connection) or
die("Couldn't Execute the query.");
// Getting the number of rows affected by the query
$rows = mysql_num_rows($result);
print("Rows in DB: $rows\n");
/*
Generating a Random number between 1 and the NUMBER OF
ROWS returned from the DB Table
*/
// Seeding the random number generator...
// this thing is usually used to SET an initial value
for the random number to be generated.
mt_srand(doubleval(microtime()) * 100000000);
// The random number is generated...
$random_number = mt_rand(1, $rows);
print("The random number generated is
$random_number");
echo "<BR><BR>";
/*
Fetching the actual data from the DB Table
*/
$query = "select banner_image, text, filetype from
banner_details where id=$random_number";
$result = mysql_query($query, $connection) or
die("Couldn't Execute the query.");
$data = @MYSQL_RESULT($result,0,"banner_image");
$text = @MYSQL_RESULT($result,0,"text");
$type = @MYSQL_RESULT($result,0,"filetype");
echo "The banner is ";
Header("Content-type: $type");
echo $data;
echo "The assocaited text is : $text";
?>
--------------
And this for calling it...
<?php
echo "<img src=rotate.php>";
?>
I get an Img with a cross (broken)...
Whats the error?
Am i missing something?
Pls help
-Monil
=====
Best Regards,
Monil Chheda(INDIA)
http://domains.eliteral.com
===========================
===========================
__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail