
CREATE TABLE `images` ( `imgid` INT NOT NULL AUTO_INCREMENT , `sixfourdata` LONGTEXT NOT NULL , PRIMARY KEY ( `imgid` ) );The READDIR.PHP script
<?
$dbcnx = mysql_connect("localhost", "username", "password");
mysql_select_db("base64imgdb");
?>
Next we need to open the directory, where
$path = "./";
$dir_handle = opendir($path) or die("Unable to open directory $path");
This is the hardest part of the script: sorting the image types, reading the data using
<?
while ($file = readdir($dir_handle)) {
$filetyp = substr($file, -3);
if ($filetyp == 'gif' OR $filetyp == 'jpg') {
$handle = fopen($path . "/" . $file,'r');
$file_content = fread($handle,filesize($path . "/" . $file));
fclose($handle);
$encoded = chunk_split(base64_encode($file_content));
$sql = "INSERT INTO images SET sixfourdata='$encoded'";
mysql_query($sql);
}
}
?>
This is the last and final part of the readdir.php: closing the directory and stating the proccess is complete:
<?
closedir($dir_handle);
echo("complete");
mysql_close($dbcnx);
?>
The Image Reader IMAGE.PHP
<?
$dbcnx = mysql_connect("localhost", "username", "password");
mysql_select_db("base64imgdb");
?>
Now we need to find out which row its requesting, which is done using <? $img = $_REQUEST["img"]; ?>After this, we need to connect to the table, get the data, and set it into variables:
<?
$result = mysql_query("SELECT * FROM images WHERE imgid=" . $img .
"");
if (!$result) {
echo("<b>Error performing query: " . mysql_error() . "</b>");
exit();
}
while ($row = mysql_fetch_array($result)) {
$imgid = $row["imgid"];
$encodeddata = $row["sixfourdata"];
}
?>
Now here is the last and most confusing part of the file:
<? mysql_close($dbcnx); echo base64_decode($encodeddata); ?>
<img src="image.php?img=1" border="0" alt="" />Now that wasnt so hard was it? But most of you are probably wondering why when you link to a page, you get an image. This is the reason: images arent defined by their 3 letter suffixes (such as jpg or gif), but by how their headers are written. IMAGE.PHP simply echos the image data, and acts like an image even though it just proccesses the request. This is why you get an image.
<?
###############################
# DB CONNECTION
# CHANGE THESE VALUES
###############################
$dbcnx = mysql_connect("localhost", "username", "password");
mysql_select_db("base64imgdb");
$path = "./";
$dir_handle = opendir($path) or die("Unable to open directory $path");
while ($file = readdir($dir_handle)) {
$filetyp = substr($file, -3);
if ($filetyp == 'gif' OR $filetyp == 'jpg') {
$handle = fopen($file,'r');
$file_content = fread($handle,filesize($file));
fclose($handle);
$encoded = chunk_split(base64_encode($file_content));
$sql = "INSERT INTO images SET sixfourdata='$encoded'";
mysql_query($sql);
}
}
closedir($dir_handle);
echo("complete");
mysql_close($dbcnx);
?>
image.php:
<?
$dbcnx = mysql_connect("localhost", "username", "password");
mysql_select_db("base64imgdb");
$img = $_REQUEST["img"];
$result = mysql_query("SELECT * FROM images WHERE imgid=" . $img . "");
if (!$result) {
echo("<b>Error performing query: " . mysql_error() . "</b>");
exit();
}
while ($row = mysql_fetch_array($result) ) {
$imgid = $row["imgid"];
$encodeddata = $row["sixfourdata"];
}
mysql_close($dbcnx);
echo base64_decode($encodeddata);
?>
And view.php (i shouldnt need to post this..)
<html> <body> .. <img src="image.php?img=1" border="0" alt="" /> .. </body> </html>
Copyright © 2000-2008 Spoono, LLC. All rights reserved.
Network: Reseller Web Hosting by Spoono Host | Spoonloads | Absolute Cross
Terms of Service | Privacy Policy.