SlideShare una empresa de Scribd logo
1 de 4
Storing images in a database

<?PHP

    $dbcnx = @MYSQL_CONNECT("localhost", "username",
"password");
    IF (!$dbcnx) {
      ECHO( "<p>connection to database server failed!</p>"
);
      EXIT();
    }
    IF (! @MYSQL_SELECT_DB("base64imgdb") ) {
      ECHO( "<p>Image Database Not Available!</p >" );
      EXIT();
    }
?>


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

$dbcnx = @MYSQL_CONNECT("localhost", "username",
"password");
     IF (!$dbcnx) {
       ECHO( "
connection to database server failed!
"
);
       EXIT();
     }
     IF (! @MYSQL_SELECT_DB("base64imgdb") ) {
       ECHO( "
Image Database Not Available!
" );
       EXIT();
     }
?>

Now we need to find out which row it's requesting, which is done
using image.php?img=x:

<?PHP
$img = $_REQUEST["img"];
?>
After this, we need to connect to the table, get the data, and
set it into variables:
<?PHP
     $result = @MYSQL_QUERY("SELECT * FROM images WHERE imgid=" . $img .
"");
     IF (!$result) {
         ECHO("
Error performing query: " . MYSQL_ERROR()
. "
");
         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:

<?PHP

ECHO BASE64_DECODE($encodeddata);

?>


Now let me explain this. All this does is decodes the base64-encoded
image data, end echos it. That's it, nothing else.


VIEW.PHP (example viewer)
Okay, so you made it this far already. This is now the easiest
to copy and paste but hardest part to understand, where
image.php?img=1 matches with whatever row the image is on, for
example if it's row 357 then you would need to put

image.php?img=357:
<img src='image.php?img=1'   border="0" alt="">

Now that wasn't 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.


readdir.php:
<?PHP

###############################
# DB CONNECTION
# CHANGE THESE VALUES
###############################
    $dbcnx = @MYSQL_CONNECT("localhost", "username",
"password");
    IF (!$dbcnx) {
ECHO( "
connection to database server failed!
"
);
       EXIT();
     }
     IF (! @MYSQL_SELECT_DB("base64imgdb") ) {
       ECHO( "
Image Database Not Available!
" );
       EXIT();
     }

$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");

?>


image.php:
<?PHP

$dbcnx = @MYSQL_CONNECT("localhost", "username",
"password");
     IF (!$dbcnx) {
       ECHO( "
connection to database server failed!
"
);
       EXIT();
     }
     IF (! @MYSQL_SELECT_DB("base64imgdb") ) {
       ECHO( "
Image Database Not Available!
" );
       EXIT();
     }

$img = $_REQUEST["img"];

       $result = @MYSQL_QUERY("SELECT * FROM images WHERE imgid=" . $img .
"");
IF (!$result) {
        ECHO("
Error performing query: " . MYSQL_ERROR()
. "
");
        EXIT();
    }
    WHILE ( $row = MYSQL_FETCH_ARRAY($result) ) {
      $imgid = $row["imgid"];
      $encodeddata = $row["sixfourdata"];
      }

ECHO BASE64_DECODE($encodeddata);
?>

view.php:
<html>
<body>
..
<img src='image.php?img=1'   border="0" alt="">
..
</body>
</html>

Más contenido relacionado

Más de karan chanana

Java script slideshow by karan chanana
Java script slideshow by karan chananaJava script slideshow by karan chanana
Java script slideshow by karan chananakaran chanana
 
Java script while loop by karan chanana
Java script while loop by karan chananaJava script while loop by karan chanana
Java script while loop by karan chananakaran chanana
 
Add text on image by karan chanana
Add text on image by karan chananaAdd text on image by karan chanana
Add text on image by karan chananakaran chanana
 
Accessing data from a simple post by karan chanana
Accessing data from a simple post by karan chananaAccessing data from a simple post by karan chanana
Accessing data from a simple post by karan chananakaran chanana
 
Adding text to an image by karan chanana
Adding text to an image by karan chananaAdding text to an image by karan chanana
Adding text to an image by karan chananakaran chanana
 
Javascript validation by karan chanana
Javascript validation by karan chananaJavascript validation by karan chanana
Javascript validation by karan chananakaran chanana
 
Add two numbers karan chanana
Add two numbers karan chananaAdd two numbers karan chanana
Add two numbers karan chananakaran chanana
 
Javascript validation karan chanana
Javascript validation karan chananaJavascript validation karan chanana
Javascript validation karan chananakaran chanana
 
Javascript validation karan
Javascript validation karanJavascript validation karan
Javascript validation karankaran chanana
 

Más de karan chanana (10)

Java script slideshow by karan chanana
Java script slideshow by karan chananaJava script slideshow by karan chanana
Java script slideshow by karan chanana
 
Java script while loop by karan chanana
Java script while loop by karan chananaJava script while loop by karan chanana
Java script while loop by karan chanana
 
Add text on image by karan chanana
Add text on image by karan chananaAdd text on image by karan chanana
Add text on image by karan chanana
 
Accessing data from a simple post by karan chanana
Accessing data from a simple post by karan chananaAccessing data from a simple post by karan chanana
Accessing data from a simple post by karan chanana
 
Adding text to an image by karan chanana
Adding text to an image by karan chananaAdding text to an image by karan chanana
Adding text to an image by karan chanana
 
Karan chanana
Karan chananaKaran chanana
Karan chanana
 
Javascript validation by karan chanana
Javascript validation by karan chananaJavascript validation by karan chanana
Javascript validation by karan chanana
 
Add two numbers karan chanana
Add two numbers karan chananaAdd two numbers karan chanana
Add two numbers karan chanana
 
Javascript validation karan chanana
Javascript validation karan chananaJavascript validation karan chanana
Javascript validation karan chanana
 
Javascript validation karan
Javascript validation karanJavascript validation karan
Javascript validation karan
 

Último

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 

Último (20)

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 

Storing images in a database by karan chanana

  • 1. Storing images in a database <?PHP $dbcnx = @MYSQL_CONNECT("localhost", "username", "password"); IF (!$dbcnx) { ECHO( "<p>connection to database server failed!</p>" ); EXIT(); } IF (! @MYSQL_SELECT_DB("base64imgdb") ) { ECHO( "<p>Image Database Not Available!</p >" ); EXIT(); } ?> 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 $dbcnx = @MYSQL_CONNECT("localhost", "username", "password"); IF (!$dbcnx) { ECHO( " connection to database server failed! " ); EXIT(); } IF (! @MYSQL_SELECT_DB("base64imgdb") ) { ECHO( " Image Database Not Available! " ); EXIT(); } ?> Now we need to find out which row it's requesting, which is done using image.php?img=x: <?PHP $img = $_REQUEST["img"]; ?> After this, we need to connect to the table, get the data, and set it into variables:
  • 2. <?PHP $result = @MYSQL_QUERY("SELECT * FROM images WHERE imgid=" . $img . ""); IF (!$result) { ECHO(" Error performing query: " . MYSQL_ERROR() . " "); 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: <?PHP ECHO BASE64_DECODE($encodeddata); ?> Now let me explain this. All this does is decodes the base64-encoded image data, end echos it. That's it, nothing else. VIEW.PHP (example viewer) Okay, so you made it this far already. This is now the easiest to copy and paste but hardest part to understand, where image.php?img=1 matches with whatever row the image is on, for example if it's row 357 then you would need to put image.php?img=357: <img src='image.php?img=1' border="0" alt=""> Now that wasn't 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. readdir.php: <?PHP ############################### # DB CONNECTION # CHANGE THESE VALUES ############################### $dbcnx = @MYSQL_CONNECT("localhost", "username", "password"); IF (!$dbcnx) {
  • 3. ECHO( " connection to database server failed! " ); EXIT(); } IF (! @MYSQL_SELECT_DB("base64imgdb") ) { ECHO( " Image Database Not Available! " ); EXIT(); } $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"); ?> image.php: <?PHP $dbcnx = @MYSQL_CONNECT("localhost", "username", "password"); IF (!$dbcnx) { ECHO( " connection to database server failed! " ); EXIT(); } IF (! @MYSQL_SELECT_DB("base64imgdb") ) { ECHO( " Image Database Not Available! " ); EXIT(); } $img = $_REQUEST["img"]; $result = @MYSQL_QUERY("SELECT * FROM images WHERE imgid=" . $img . "");
  • 4. IF (!$result) { ECHO(" Error performing query: " . MYSQL_ERROR() . " "); EXIT(); } WHILE ( $row = MYSQL_FETCH_ARRAY($result) ) { $imgid = $row["imgid"]; $encodeddata = $row["sixfourdata"]; } ECHO BASE64_DECODE($encodeddata); ?> view.php: <html> <body> .. <img src='image.php?img=1' border="0" alt=""> .. </body> </html>