Image upload with MYSQL database

PHP, Programing Add comments

 In this tutorial is presented a simple code with informations about programing a script that uses MYSQL database to upload images. Very simple to interpretate, to code and to test, its a simple way to learn how to program a upload script that select images from the MYSQL database using php programing. Image upload with MYSQL databaseThere will be 3 PHP files in this tutorial: readdir.php - this puts all the images in a folder into the database image.php - the actual image script that displays the imag view.php - an example file that shows you how to call the imageCreating the Image DatabaseFirst, create a mysql database called base64imgdb (this is the name that will be used throughout the tutorial)Second, create a table called images with two rows. Name the first one imgid, and give it the parameters TYPE: INT EXTRA: auto_increment, and check the circle under Primary. Name the second sixfourdata, and make it TYPE: LONGTEXT. Here is the sql code:

MySQL:
  1. <br />
  2. CREATE TABLE `images` (<br />
  3. `imgid` INT NOT NULL AUTO_INCREMENT ,<br />
  4. `sixfourdata` LONGTEXT NOT NULL ,<br />
  5. PRIMARY KEY ( `imgid` )<br />
  6. );<br />

The READDIR.PHP script
This script reads a directory within the server, selects all the jpg and gif images, encodes them into base64, and uploads them to the database, except in a different order. This is because the script reads each image in a loop, and we would like to keep a constant connection to the mysql database instead of creating multiple ones. Here is the database connection where: username and password

is the directory the readdir.php file is located:

PHP:
  1. <br />
  2. $path = "./";<br />
  3. $dir_handle = opendir($path) or die("Unable to open directory $path");<br />

PHP:
  1. <br />
  2. <?<br />
  3. while ($file = readdir($dir_handle)) {<br />
  4. $filetyp = substr($file, -3);<br />
  5. if ($filetyp == 'gif' OR $filetyp == 'jpg') {<br />
  6. $handle = fopen($path . "/" . $file,'r');<br />
  7. $file_content = fread($handle,filesize($path . "/" . $file));<br />
  8. fclose($handle);<br />
  9. $encoded = chunk_split(base64_encode($file_content));<br />
  10. $sql = "INSERT INTO images SET sixfourdata='$encoded'";<br />
  11. mysql_query($sql);<br />
  12. }<br />
  13. }<br />
  14. ?><br />

This is the last and final part of the readdir.php: closing the directory and stating the proccess is complete:

PHP:
  1. <br />
  2. <?<br />
  3. closedir($dir_handle);<br />
  4. echo("complete");<br />
  5. mysql_close($dbcnx);<br />
  6. ?><br />

The Image Reader IMAGE.PHP
This file may be the hardest file to understand whenever you see how simple view.php is, but bear with me, your patience will pay off. This file takes a request, requests the row in the table, decodes the data, and presents itself as an image. First, we have to connect to the database again:

PHP:
  1. <br />
  2. <?<br />
  3. $dbcnx = mysql_connect("localhost", "username", "password");<br />
  4. mysql_select_db("base64imgdb");<br />
  5. ?><br />

After this, we need to connect to the table, get the data, and set it into variables:

PHP:
  1. <br />
  2. <?<br />
  3.     $result = mysql_query("SELECT * FROM images WHERE imgid=" . $img .<br />
  4. "");<br />
  5.     if (!$result) {<br />
  6.       echo("<b>Error performing query: " . mysql_error() . "</b>");<br />
  7.       exit();<br />
  8.     }<br />
  9.     while ($row = mysql_fetch_array($result)) {<br />
  10.     $imgid = $row["imgid"];<br />
  11.     $encodeddata = $row["sixfourdata"];<br />
  12.     }<br />
  13. ?><br />

Now here is the last and most confusing part of the file:
[/php]
mysql_close($dbcnx);
echo base64_decode($encodeddata);
?>

The files:
readdir.php:

PHP:
  1. <br />
  2. <?<br />
  3. ###############################<br />
  4. # DB CONNECTION<br />
  5. # CHANGE THESE VALUES<br />
  6. ###############################<br />
  7. $dbcnx = mysql_connect("localhost", "username", "password");<br />
  8. mysql_select_db("base64imgdb");</p>
  9. <p>$path = "./";<br />
  10. $dir_handle = opendir($path) or die("Unable to open directory $path");</p>
  11. <p>while ($file = readdir($dir_handle)) {<br />
  12. $filetyp = substr($file, -3);<br />
  13. if ($filetyp == 'gif' OR $filetyp == 'jpg') {<br />
  14. $handle = fopen($file,'r');<br />
  15. $file_content = fread($handle,filesize($file));<br />
  16. fclose($handle);<br />
  17. $encoded = chunk_split(base64_encode($file_content));<br />
  18. $sql = "INSERT INTO images SET sixfourdata='$encoded'";<br />
  19. mysql_query($sql);<br />
  20. }<br />
  21. }</p>
  22. <p>closedir($dir_handle);<br />
  23. echo("complete");<br />
  24. mysql_close($dbcnx);<br />
  25. ?><br />

image.php:

PHP:
  1. <br />
  2. <?<br />
  3. $dbcnx = mysql_connect("localhost", "username", "password"); </p>
  4. <p>mysql_select_db("base64imgdb");</p>
  5. <p>$img = $_REQUEST["img"];</p>
  6. <p>    $result = mysql_query("SELECT * FROM images WHERE imgid=" . $img . "");<br />
  7.     if (!$result) {<br />
  8.       echo("<b>Error performing query: " . mysql_error() . "</b>");<br />
  9.       exit();<br />
  10.     }<br />
  11.     while ($row = mysql_fetch_array($result) ) {<br />
  12.     $imgid = $row["imgid"];<br />
  13.     $encodeddata = $row["sixfourdata"];<br />
  14.     }<br />
  15. mysql_close($dbcnx);<br />
  16. echo base64_decode($encodeddata);<br />
  17. ?><br />

Comments are closed.