PHP Tutorial
PHP Tutorial
PHP Tutorial
compatible
PHP 4.3.0 or higher
PHP 5
generates HTML code for the list of uploaded photos, forms for photo deletion
and uploading
image uploading –
image showing –
Fetches image information from MySQL database and sends image do browser. If
PHP is installed as mod_php (for Apache), does If-Modified-Since HTTP header
checking.
Image gallery example uses following table to store all of its data:
< ?php
$db_host = 'localhost'; // don't forget to change
$db_user = 'mysql-user';
$db_pwd = 'mysql-password';
$database = 'test';
$table = 'ae_gallery';
// use the same name as SQL table
$password = '123';
// simple upload restriction,
// to disallow uploading to everyone
if (!mysql_select_db($database))
die("Can't select database");
return mysql_real_escape_string($s);
}
if (mysql_num_rows($result) == 0)
die('no image');
$send_304 = false;
if (php_sapi_name() == 'apache') {
// if our web server is apache
// we get check HTTP
// If-Modified-Since header
// and do not send image
// if there is a cached version
$ar = apache_request_headers();
if (isset($ar['If-Modified-Since']) && // If-Modified-Since
should exists
($ar['If-Modified-Since'] != '') && // not empty
(strtotime($ar['If-Modified-Since']) >= $image_time)) // and
grater than
$send_304 = true; //
image_time
}
if ($send_304)
{
// Sending 304 response to browser
// "Browser, your cached version of image is OK
// we're not sending anything new to you"
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $ts).' GMT',
true, 304);
exit(); // bye-bye
}
// outputing image
echo $data;
exit();
}
?>
<html><head>
<title>MySQL Blob Image Gallery Example</title>
</head>
<body>
< ?php
if (isset($msg)) // this is special section for
// outputing message
{
?>
<p style="font-weight: bold;">< ?=$msg?>
<br />
<a href="<?=$PHP_SELF?>">reload page</a>
<!-- I've added reloading link, because
refreshing POST queries is not good idea -->
</p>
< ?php
}
?>
<h1>Blob image gallery</h1>
<h2>Uploaded images:</h2>
<form action="<?=$PHP_SELF?>" method="post">
<!-- This form is used for image deletion -->
< ?php
$result = mysql_query("SELECT id, image_time, title FROM {$table} ORDER
BY id DESC");
if (mysql_num_rows($result) == 0) // table is empty
echo '<ul><li>No images loaded</li>';
else
{
echo '<ul>';
while(list($id, $image_time, $title) = mysql_fetch_row($result))
{
// outputing list
echo "<li><input type='radio' name='del' value='{$id}'/>";
echo "<a href="http://mistonline.in/wp/php-mysql-example-image-
gallery-blob-storage/">{$title}</a> – ";
echo "<small>{$image_time}</small></li>";
}
echo '</ul>';
</form>
<h2>Upload new image:</h2>
<form action="<?=$PHP_SELF?>" method="POST" enctype="multipart/form-
data">
<label for="title">Title:</label><br />
<input type="text" name="title" id="title" size="64"/><br /><br />
MySQL
* mysql_connect – connects to MySQL server
* mysql_select_db – select database
* mysql_query – send query
* mysql_fetch_row – get current row from result table
* mysql_real_escape_string – escaping string to use it in MySQL query
* mysql_num_fields – get number of rows
PHP