Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2009
    Posts
    13

    converting a blob back to an image!

    hi since my last post on here i've been working hard and learning a fair a bit of php (im no master obviously, but im still enjoying it). however im close to pulling my hair out now.
    heres my problem, im storing small images on my database as a mediumblob which for the website im trying to build is my best option. all i want to do is loop echo out all the images from the database below my form but i keep getting all the encrypted text not the image..... i've tried just about every online tut i can find now and looked in books but no joy. any help would be amazing

    heres my create table

    CREATE TABLE `upload` (
    `id` int(11) NOT NULL auto_increment,
    `name` varchar(30),
    `type` varchar(30),
    `size` int(11),
    `content` mediumblob,
    PRIMARY KEY (`id`)
    )


    the upload code im using

    <?php
    $db_link = mysql_connect("localhost", "testing", "testing");
    mysql_select_db("testing");

    if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
    {
    $fileName = $_FILES['userfile']['name'];
    $tmpName = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];

    $fp = fopen($tmpName, 'r');
    $content = fread($fp, filesize($tmpName));
    $content = addslashes($content);
    fclose($fp);

    if(!get_magic_quotes_gpc()) {
    $fileName = addslashes($fileName);}

    $query = "INSERT INTO upload (name, size, type, content ) ".
    "VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

    mysql_query($query) or die('Error, query failed');

    echo "<br>File $fileName uploaded<br>";
    }
    ?>

    <form method="post" enctype="multipart/form-data">
    <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
    <tr>
    <td width="246">
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
    <input name="userfile" type="file" id="userfile">
    </td>
    <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
    </tr>
    </table>
    </form>

    ..........................

    i just can't find anything to add on the end that will work!!!

    any help would be great
    thank you
    stephen

  2. #2
    Join Date
    Mar 2003
    Location
    Denmark
    Posts
    2,882
    Im no php wizzard anymore, but you say you are storing the actual image data in the databse ? Why ?
    Why not just store information about a FILE in the database and leave the image in some normal image format on the server ?
    Swoop - Buzzed Pirate!
    " If nobodys talking to me, im talking to me" - Swoop

    Reflection Design || Swoops Blog

    2D Tutorials Compilation

    The Speed Painting Thread || ((post your Concept Art thread))


  3. #3
    Join Date
    Dec 2009
    Posts
    13
    Hi swoop

    i agree that would be the best way to go about it, just storing the info so I can pull it up from the server. However the website is for my dad, and he struggles with computers and as far as I can make out storing the image as a blob is the only way I can build a admin page later on that can delete the image when he’s done, rather than him deleting the info in a admin page then having to manually delete the image from the server which I think he would struggle with.

  4. #4
    Join Date
    Mar 2003
    Location
    Denmark
    Posts
    2,882
    Why not have the admin page handle files also ?
    My friend built an image handling plugin for our CMS system and it simply uploads files, and deletes them on commands from the GUI.

    If you do a quick search on how to upload images to server using php, you can find examples of how to easily add images. And then you simply reverse the process if you need to delete. He doesnt need to manually clean up afterwards.. have your code do the work
    Swoop - Buzzed Pirate!
    " If nobodys talking to me, im talking to me" - Swoop

    Reflection Design || Swoops Blog

    2D Tutorials Compilation

    The Speed Painting Thread || ((post your Concept Art thread))


  5. #5
    Join Date
    Mar 2003
    Location
    Denmark
    Posts
    2,882
    Guessing you can use this function:

    http://www.php.net/manual/en/function.unlink.php
    Swoop - Buzzed Pirate!
    " If nobodys talking to me, im talking to me" - Swoop

    Reflection Design || Swoops Blog

    2D Tutorials Compilation

    The Speed Painting Thread || ((post your Concept Art thread))


  6. #6
    Join Date
    Dec 2009
    Posts
    13
    hmmm sounds good swoop, i'll have a look about and see what i come up with.
    many thanks
    i'll keep you posted with what i get

    ste

  7. #7
    Join Date
    Mar 2003
    Location
    Denmark
    Posts
    2,882
    No problem. If you get stuck let me know. My teammate is a php wiz and should easily be able to fix you up some examples of how it could work
    Swoop - Buzzed Pirate!
    " If nobodys talking to me, im talking to me" - Swoop

    Reflection Design || Swoops Blog

    2D Tutorials Compilation

    The Speed Painting Thread || ((post your Concept Art thread))


  8. #8
    Join Date
    Apr 2004
    Location
    Nr London, UK
    Posts
    831
    If you save the data, you will need to output it with the correct content type

    save the content type of the image then output that

    Code:
    header('Content-Type: image/jpeg');
    echo $jpegData;
    or

    Code:
    header('Content-Type: image/png');
    echo $pngData;
    Edit: but yes, file serving from files is naturally faster, since it doesn't have a seperate handler first..

    unlink() will delete the file as swoop said.

    if you upload manually and then try and unlink and your user account to upload was different to the web server process then you can encounter permissions issues - just a heads up, so for cleanlyness, its best to do it one way

  9. #9
    Join Date
    Dec 2009
    Posts
    13
    thanks for the advice guys, looks like im on a winner with unlink() so thats the way im going. im writing an admin page script at the moe and all is going well. when im done if anyone wants me to, i'll post it up so you can have a look. im aware it's probably a pretty simple script for most people on here tho ha ha

    many thanks guys

Posting Permissions

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