MySQL
  Home arrow MySQL arrow Page 6 - Blobbing Data With PHP and MySQL
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Blobbing Data With PHP and MySQL
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 276
    2002-02-05

    Table of Contents:
  • Blobbing Data With PHP and MySQL
  • Creating the database
  • Adding blobs to the database
  • The grabfile.php script
  • Displaying the files
  • The downloadfile.php script
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Blobbing Data With PHP and MySQL - The downloadfile.php script


    (Page 6 of 7 )

    As mentioned on the previous page, when we click on the "Download now" link, the downloadfile.php script will be called up, passing the id of the blob requested as the blobId query string value. Create a new file named downloadfile.php and enter the following code into it:

    <?php

    global $blobId;



    if(!is_numeric($blobId))

    die("Invalid blobId specified");

    // Database connection variables

    $dbServer = "localhost";

    $dbDatabase = "myFiles";

    $dbUser = "admin";

    $dbPass = "password";



    $sConn = mysql_connect($dbServer, $dbUser, $dbPass)

    or die("Couldn't connect to database server");



    $dConn = mysql_select_db($dbDatabase, $sConn)

    or die("Couldn't connect to database $dbDatabase");



    $dbQuery = "SELECT blobType, blobData ";

    $dbQuery .= "FROM myBlobs ";

    $dbQuery .= "WHERE blobId = $blobId";

    $result = mysql_query($dbQuery) or die("Couldn't get file list");



    if(mysql_num_rows($result) == 1)

    {

    $fileType = @mysql_result($result, 0, "blobType");

    $fileContent = @mysql_result($result, 0, "blobData");



    header("Content-type: $fileType");

    echo $fileContent;

    }

    else

    {

    echo "Record doesn't exist.";

    }

    ?>


    In the code above we use the query string variable $blobId to retrieve the blobType and blobData fields from our myBlobs database. If there was a row returned from our query, then we save its type and actual data to two variables, called $fileType and $fileContent respectively.

    We use the header function to change the type of content that we are sending to the browser. The default content type is text/html, which the browser assumes is a web page. If we didn’t include a call to the header function, then when we output our binary data it will look like garbage in the browser. Lastly, we use the echo command to actually output the contents of our binary file to the browser, which handles the file as it sees fit.

    Take a look at the screen shots below for how the script works with different file types:

    For Images:

    Displays the image in the browser

    For HTML documents:

    Displays the HTML as normal

    For MS Word documents:

    Displays the MS Word document in the browser

    More MySQL Articles
    More By Mitchell Harper


       · AM devolping a website in which am using PHP and MYSQL, and here any member can...
       · this will not transfer the variable $_FILE. After a bit of research i found if you...
       · Not seeing this "Support Material" I keep seeing.. Can someone direct me to it?
       · I am on Apache 2.0 server. When I enter the grabfile page I do not see how the...
     

    MYSQL ARTICLES

    - MySQL and BLOBs
    - Two Lessons in ASP and MySQL
    - Lord Of The Strings Part 2
    - Lord Of The Strings Part 1
    - Importing Data into MySQL with Navicat
    - Building a Sustainable Web Site
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - PhpED 3.2 – More Features Than You Can Poke ...
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - Security and Sessions in PHP
    - Setup Your Personal Reminder System Using PHP
    - Create a IP-Country Database Using PERL and ...
    - Developing a Dynamic Document Search in PHP ...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT