Closed Thread
Page 11 of 22 FirstFirst ... 91011121321 ... LastLast
Results 101 to 110 of 214

Thread: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

  1. #101
    madhaninfo is offline Newbie
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    it works but some time opens in dreamweaver.i just used the same code

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #102
    ezcat is offline Learning Programmer
    Join Date
    Nov 2008
    Posts
    39
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    I have used this tutorial to save and display images. Works great, thanks.
    Just a quick note for anyone that is using this to store PDF images.
    Change the header in show.php to:
    header('Content-type: application/pdf');
    Great tutorial!

    Jon

  4. #103
    Jaan Guest

    Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    You're welcome.. but yes, but if you wanna host lot's of different files in db.. then store their mime type into db also.. and then automatically take it from db so you don't have to change this header thing.. just take mime type from db and add it to header like this:

    Code:
    $mime_type "MimeTypeTakenFromDb";
    header("Content-type: $mime_type"); 

  5. #104
    ezcat is offline Learning Programmer
    Join Date
    Nov 2008
    Posts
    39
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    Yes, thanks. I have added a field in the DB for file type.

    Jon

  6. #105
    phillw's Avatar
    phillw is offline Learning Programmer
    Join Date
    Aug 2009
    Location
    Northwest England (UK)
    Posts
    82
    Blog Entries
    1
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    Hi Jaan, I've just had a quick google, before I ask the question. As headers have to be sent as, well, headers - Is it okay to include multiple Content-Types ?

    Thanks,

    Phill.

  7. #106
    Jaan Guest

    Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    Why do you need multiple Content-Types ?

  8. #107
    phillw's Avatar
    phillw is offline Learning Programmer
    Join Date
    Aug 2009
    Location
    Northwest England (UK)
    Posts
    82
    Blog Entries
    1
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    Quote Originally Posted by Jaan View Post
    Why do you need multiple Content-Types ?
    Say I have a mix of jpg & pdf etc.

    How should that be declared ?

    Thanks,

    Phill.

  9. #108
    k1net1cs is offline Newbie
    Join Date
    Oct 2009
    Posts
    20
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    Quote Originally Posted by phillw View Post
    Say I have a mix of jpg & pdf etc.

    How should that be declared ?
    What is the general layout for that "mix"?
    You can just write a PHP file that handles images/files depending on the URL parameters.

    Let's just assume the PHP file is named getfile.php:

    Code:
    <?php
      
    // Assuming $_GET["type"] is the file type,
      // $_GET["id"] is the file id in the database,
      // and the URL to call this file is something like
      // getfile.php?type=img&id=8
      // or getfile.php?type=pdf&id=10

      
    if (isset($_GET["type"]) && isset($_GET["id"])) {
        if (
    $_GET["type"] == "img" && is_numeric($_GET["id"])) {
          
    // Put the code required for pulling the image from the database.
          // It's better to also have the mime-type stored in the database.
          // Put that mime-type into a variable called $imgMime.
          // And assume the image blob is $imgData.
          
          
    header("Content-type: $imgMime");
          
          echo 
    $imgData;
          
          
    mysql_close();
        }
        else if (
    $_GET["type"] == "pdf" && is_numeric($_GET["id"])) {
          
    // Now, assuming you want the user to download the PDF file,
          // the header setting would be a bit different.
          // This is assuming the mime-type is $fileMime,
          // you stored the file size in the db and is being put into $fileSize,
          // you stored the file name in the db and is being put into $fileName,
          // and the data blob is $fileData.
          
          
    header("Content-Type: $fileMime");
          
    header("Content-Length: $fileSize");
          
    header("Content-Disposition: attachment; filename=$fileName");
          
          echo 
    $fileData;
          
          
    mysql_close();
        }
      }
    ?>
    Remember that you can always add more conditionals for any file types you might need.

    Now, the HTML file should be something like this if you want to make the PHP file works:

    HTML Code:
    <html>
    <head>
      <title>Getting some files from MySQL blobs</title>
    </head>
    <body>
      <table>
        <tr>
          <td><img src="getfile.php?type=img&id=1" alt=""></td>
          <td><a href="getfile.php?type=pdf&id=1">Click here to download the PDF!</a></td>
        </tr>
      </table>
    </body>
    </html>
    Of course, the ideal way is to generate the HTML and then the file types and IDs iteratively using another PHP file.

    Hope that helps.

    ...oh, and please correct me if I'm wrong.

  10. #109
    phillw's Avatar
    phillw is offline Learning Programmer
    Join Date
    Aug 2009
    Location
    Northwest England (UK)
    Posts
    82
    Blog Entries
    1
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    Thanks ....

    hmmmm.... Things have gotten a bit more complicated, but, as I declare in a php script my HTML headers, I can have a 'play' with it and see if it passes XHTML (Strict).

    What I was asking was, if I have JPG / PNG stuff and PDF stuff being built by my php script to display in strictly mode, how do I achieve it - I can only send the header once.

    I'm glad most of the pages require jpg's being transferred - the header file for MIME type is growing

    but, i do reduce the code for each page as I transfer control over the php routine

    The last fully commented mime-type module I did is here Mime types with dynamic web-sites - VPOLink

    Since then, I'm introducing Rule508 into the system - fortunately xhtml-strict is pretty darn compliant with it.

    Phill.
    Last edited by phillw; 10-30-2009 at 07:46 PM.

  11. #110
    k1net1cs is offline Newbie
    Join Date
    Oct 2009
    Posts
    20
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    Quote Originally Posted by phillw View Post
    hmmmm.... Things have gotten a bit more complicated, but, as I declare in a php script my HTML headers, I can have a 'play' with it and see if it passes XHTML (Strict).

    What I was asking was, if I have JPG / PNG stuff and PDF stuff being built by my php script to display in strictly mode, how do I achieve it - I can only send the header once.

    I'm glad most of the pages require jpg's being transferred - the header file for MIME type is growing

    but, i do reduce the code for each page as I transfer control over the php routine
    Actually, it doesn't matter if the PHP file can only send the header once, since you always "re-run" the "file fetching PHP file" every time you "call" it using the bog standard <img src="" alt="" /> tag.

    That's why I said earlier in my previous post that you should use the PHP file just to fetch the files you wanted along with their proper mime-types, which means it's not to also be used to generate the XHTML page.

    Basically, you use one PHP file to generate the XHTML file and another just to fetch the files.
    That file fetching PHP will always be run multiple times according to how much, for example, images that you want to show in the page, regardless of its mime-types.
    This file fetching PHP is also the one that uses the header() function, not the XHTML generating one.

    It'd be easier to have a table that's, for example, named fileStore that at least contains these fields:
    fileId, fileName, fileMime, fileSize, fileData
    Keep in mind that fileSize is in bytes, so you might want to use BIGINT for it just to be safe, unless you can be more stringent in limiting the upload size.

    And then use another PHP file to upload and automatically generate the mime-types to be inserted to the database so that you won't have to insert them manually every time for each uploaded file.

    This is what I personally use (let's just name it upload.php):

    Code:
    <?php
    if (isset($_FILES["uploads"])) {
      
      
    // Takes all the necessary data from the uploaded image.
      
      
    $fileName mysql_real_escape_string($_FILES["uploads"]["name"]);
      
    $fileMime mysql_real_escape_string($_FILES["uploads"]["type"]);
      
    $fileSize $_FILES["uploads"]["size"];
      
    $fileData mysql_real_escape_string(file_get_contents($_FILES["uploads"]["tmp_name"]));
      
      if (
    $_FILES["uploads"]["error"] == 0) {
        
    // As usual, place the db connection code here.
        
        
    $sql "INSERT INTO `fileStore` VALUES ".
               
    "(null,'$fileName','$fileMime','$fileSize','$fileData')";
        
        
    mysql_query($sql);
        
        
    mysql_close();
      }
    }
    ?>
    As for the XHTML file to upload the files, just use a plain form for it.

    HTML Code:
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN">
    
    <html>
      <head>
        <title>File Uploader</title>
      </head>
      <body>
        <form action="upload.php" method="post" enctype="multipart/form-data">
          <table>
          <tbody>
            <tr>
              <td><input type="file" name="uploads" /></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td align="center"><input type="submit" id="submit" value="Upload" /></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
            </tr>
          </tbody>
          </table>
        </form>
      </body>
    </html>
    Yes, it lacks the DTD and xmlns part, but that's caused by the forum's limitation to members with less than 10 posts.

    After that, you can just make a PHP file to generate the HTML file that will show the images stored.
    Remember that this example uses a slightly modified getfile.php I mentioned in the previous post; I'll put the modified version after this one.

    imgshow.php
    Code:
    <?php
      
    echo <<<HEADER
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN">

    <html>
      <head>
        <title>Image Displayer</title>
      </head>
      <body>
    HEADER;

    // Put the recursive call to generate the images here.
    // Maybe something like this:

    // Place db connection code here.

    $sql "SELECT `fileId` FROM `fileStore`";

    $result mysql_query($sql);

    for (
    $row mysql_fetch_assoc($result)) {
      echo 
    "<img src=\"getfile.php?&id=".
           
    $row["fileId"].
           
    "\" /><br /><br />\n";
    }

    mysql_close();

    echo <<<FOOTER
      </body>
    </html>
    FOOTER;

    ?>
    getfile.php
    Code:
    <?php
      
    if (isset($_GET["id"])) {
        if (
    is_numeric($_GET["id"])) {
          
          
    // Place db connection code here.
          
          
    $id $_GET["id"];
          
    $sql "SELECT * FROM `fileStore` WHERE `fileId`=$id";
          
          
    $result mysql_query($sql);
          
          if (
    $row mysql_fetch_assoc($result)) {
            
    $mime $row["fileMime"];
            
    header("Content-type: $mime");
            echo 
    $row["fileData"];
          }
          
          
    mysql_close();
        }
      }
    ?>
    Well, since the file uploading part can handle any file types already and their respective mime-types are being stored in the db, it depends on how much you could tweak getfile.php to get the file types you wanted to be fetched.

    I hope I didn't misunderstand on how you want to generate your pages.
    Otherwise, my examples above may just be used as a template for anyone else interested.
    Last edited by k1net1cs; 03-16-2010 at 05:05 AM.

Closed Thread
Page 11 of 22 FirstFirst ... 91011121321 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 02-07-2011, 11:29 AM
  2. Tutorial: Storing Images in MySQL with PHP
    By Jordan in forum PHP Tutorials
    Replies: 47
    Last Post: 01-04-2011, 08:37 PM
  3. Beginner Storing images in XML file - Part II - Displaying image
    By Jaan in forum PHP Tutorials
    Replies: 6
    Last Post: 01-04-2011, 08:35 PM
  4. display images sequentially using PHP and MySQL
    By jhanjon in forum PHP Development
    Replies: 2
    Last Post: 10-08-2009, 12:11 PM
  5. Replies: 2
    Last Post: 07-16-2009, 12:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts