SlideShare une entreprise Scribd logo
1  sur  4
Télécharger pour lire hors ligne
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>

Contenu connexe

Plus 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
 

Plus 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
 

Dernier

Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxMadhavi Dharankar
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Comparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptxComparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptxAvaniJani1
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6Vanessa Camilleri
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineCeline George
 
Employablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxEmployablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxryandux83rd
 
An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPCeline George
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Celine George
 

Dernier (20)

Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptx
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Comparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptxComparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptx
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,
 
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command Line
 
Employablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxEmployablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptx
 
An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERP
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17
 

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>