SlideShare uma empresa 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>

Mais conteúdo relacionado

Destaque

Tugas uts simsp website bina marga
Tugas uts simsp website bina margaTugas uts simsp website bina marga
Tugas uts simsp website bina margaEsha S
 
Commissioner Bridget Gainer: Cook County Pension Committee Meeting - June 29,...
Commissioner Bridget Gainer: Cook County Pension Committee Meeting - June 29,...Commissioner Bridget Gainer: Cook County Pension Committee Meeting - June 29,...
Commissioner Bridget Gainer: Cook County Pension Committee Meeting - June 29,...Cook County Commissioner Bridget Gainer
 
SERVICE Magazine 20.1: Afwaarderen op vastgoed
SERVICE Magazine 20.1: Afwaarderen op vastgoedSERVICE Magazine 20.1: Afwaarderen op vastgoed
SERVICE Magazine 20.1: Afwaarderen op vastgoedSERVICE_Magazine
 
Freelancing For Non-IT Professionals
Freelancing For Non-IT ProfessionalsFreelancing For Non-IT Professionals
Freelancing For Non-IT ProfessionalsNasir Uddin Shamim
 
La Gestión de imagen como factor clave del posicionamiento
La Gestión de imagen como factor clave del posicionamientoLa Gestión de imagen como factor clave del posicionamiento
La Gestión de imagen como factor clave del posicionamientoMerecedessalas
 
MR STUCHBERY'S TOP FIVE TEXT ANALYSIS TIPS.
MR STUCHBERY'S TOP FIVE TEXT ANALYSIS TIPS.MR STUCHBERY'S TOP FIVE TEXT ANALYSIS TIPS.
MR STUCHBERY'S TOP FIVE TEXT ANALYSIS TIPS.Mike Stuchbery
 
Straetus incasso 's-Gravenhage
Straetus incasso 's-GravenhageStraetus incasso 's-Gravenhage
Straetus incasso 's-Gravenhagegbarnhoorn
 
ScambioCasa.com: Press conference in MILANO, 9 of APRIL 2015
ScambioCasa.com: Press conference in MILANO, 9 of APRIL 2015ScambioCasa.com: Press conference in MILANO, 9 of APRIL 2015
ScambioCasa.com: Press conference in MILANO, 9 of APRIL 2015Cristina Pagetti
 
Quick Wins and Shortcuts to Get the Most out of IDEA
Quick Wins and Shortcuts to Get the Most out of IDEAQuick Wins and Shortcuts to Get the Most out of IDEA
Quick Wins and Shortcuts to Get the Most out of IDEAAuditWare Systems Ltd.
 
Auditoria de area de informatica
Auditoria  de area de informaticaAuditoria  de area de informatica
Auditoria de area de informaticaEdwin Chavarria
 
Armazenamento, Indexação e Recuperação de Informação
Armazenamento, Indexação e Recuperação de InformaçãoArmazenamento, Indexação e Recuperação de Informação
Armazenamento, Indexação e Recuperação de InformaçãoMário Monteiro
 

Destaque (16)

Commissioner Bridget Gainer - January 8 2013 newsletter
Commissioner Bridget Gainer - January 8 2013 newsletterCommissioner Bridget Gainer - January 8 2013 newsletter
Commissioner Bridget Gainer - January 8 2013 newsletter
 
Tugas uts simsp website bina marga
Tugas uts simsp website bina margaTugas uts simsp website bina marga
Tugas uts simsp website bina marga
 
Commissioner Bridget Gainer: Cook County Pension Committee Meeting - June 29,...
Commissioner Bridget Gainer: Cook County Pension Committee Meeting - June 29,...Commissioner Bridget Gainer: Cook County Pension Committee Meeting - June 29,...
Commissioner Bridget Gainer: Cook County Pension Committee Meeting - June 29,...
 
SERVICE Magazine 20.1: Afwaarderen op vastgoed
SERVICE Magazine 20.1: Afwaarderen op vastgoedSERVICE Magazine 20.1: Afwaarderen op vastgoed
SERVICE Magazine 20.1: Afwaarderen op vastgoed
 
Freelancing For Non-IT Professionals
Freelancing For Non-IT ProfessionalsFreelancing For Non-IT Professionals
Freelancing For Non-IT Professionals
 
La Gestión de imagen como factor clave del posicionamiento
La Gestión de imagen como factor clave del posicionamientoLa Gestión de imagen como factor clave del posicionamiento
La Gestión de imagen como factor clave del posicionamiento
 
MR STUCHBERY'S TOP FIVE TEXT ANALYSIS TIPS.
MR STUCHBERY'S TOP FIVE TEXT ANALYSIS TIPS.MR STUCHBERY'S TOP FIVE TEXT ANALYSIS TIPS.
MR STUCHBERY'S TOP FIVE TEXT ANALYSIS TIPS.
 
Vitamnin k
Vitamnin kVitamnin k
Vitamnin k
 
Straetus incasso 's-Gravenhage
Straetus incasso 's-GravenhageStraetus incasso 's-Gravenhage
Straetus incasso 's-Gravenhage
 
ScambioCasa.com: Press conference in MILANO, 9 of APRIL 2015
ScambioCasa.com: Press conference in MILANO, 9 of APRIL 2015ScambioCasa.com: Press conference in MILANO, 9 of APRIL 2015
ScambioCasa.com: Press conference in MILANO, 9 of APRIL 2015
 
Quick Wins and Shortcuts to Get the Most out of IDEA
Quick Wins and Shortcuts to Get the Most out of IDEAQuick Wins and Shortcuts to Get the Most out of IDEA
Quick Wins and Shortcuts to Get the Most out of IDEA
 
Slide2 milikan
Slide2 milikanSlide2 milikan
Slide2 milikan
 
PIU' VELOCI DELLA LUCE
PIU' VELOCI DELLA LUCEPIU' VELOCI DELLA LUCE
PIU' VELOCI DELLA LUCE
 
Project Management
Project ManagementProject Management
Project Management
 
Auditoria de area de informatica
Auditoria  de area de informaticaAuditoria  de area de informatica
Auditoria de area de informatica
 
Armazenamento, Indexação e Recuperação de Informação
Armazenamento, Indexação e Recuperação de InformaçãoArmazenamento, Indexação e Recuperação de Informação
Armazenamento, Indexação e Recuperação de Informação
 

Mais de karan chanana

Javascript validation by karan chanana
Javascript validation by karan chananaJavascript validation by karan chanana
Javascript validation by karan chananakaran chanana
 
Load image of choice by karan chanana
Load image of choice by karan chananaLoad image of choice by karan chanana
Load image of choice by karan chananakaran chanana
 
Calendar html code by karan chanana
Calendar html code by karan chananaCalendar html code by karan chanana
Calendar html code 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
 
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
 
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
 
Java script errors by karan chanana
Java script errors by karan chananaJava script errors by karan chanana
Java script errors 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
 
Calendar html code by karan chanana
Calendar html code by karan chananaCalendar html code by karan chanana
Calendar html code by karan chananakaran chanana
 

Mais de karan chanana (11)

Javascript validation by karan chanana
Javascript validation by karan chananaJavascript validation by karan chanana
Javascript validation by karan chanana
 
Load image of choice by karan chanana
Load image of choice by karan chananaLoad image of choice by karan chanana
Load image of choice by karan chanana
 
Calendar html code by karan chanana
Calendar html code by karan chananaCalendar html code by karan chanana
Calendar html code 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
 
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
 
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
 
Java script errors by karan chanana
Java script errors by karan chananaJava script errors by karan chanana
Java script errors 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
 
Karan chanana
Karan chananaKaran chanana
Karan chanana
 
Calendar html code by karan chanana
Calendar html code by karan chananaCalendar html code by karan chanana
Calendar html code by karan chanana
 
Karan is an indian
Karan is an indianKaran is an indian
Karan is an indian
 

Último

ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
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
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17Celine George
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
CHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxCHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxAneriPatwari
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
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
 
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
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptxAneriPatwari
 

Último (20)

ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
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...
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
CHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxCHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptx
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
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
 
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
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptx
 

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>