Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2004
    Posts
    58

    Unanswered: upload image in mysql database

    Hello gys,

    I use the following code to store image in mysql databse..

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <?php
    if($_POST)
    {
    if(!is_array($_FILES) || !is_array($_FILES['visitor_photo']))
    {
    die("no file was uploaded");
    }
    $tmp = $_FILES['visitor_photo']['tmp_name'];
    $fp = fopen($tmp, 'rb');
    $image = fread($fp, filesize($tmp));
    fclose($fp);
    $name = $_POST['visitor_name'];
    $db = @mysql_connect("localhost", "myusername", "mypassword") or die (mysql_error());
    @mysql_select_db("competence", $db) or die (mysql_error());

    // gzip file
    //$image = gzdeflate($image, 9);
    $image = mysql_real_escape_string($image, $db);
    $result = @mysql_query("INSERT INTO `tb_member` (`nom`, `photo`) VALUES ('$name' ,'$image')", $db);

    if($result === false)
    {
    die("could not insert into database");
    }
    unlink($tmp);
    exit("upload successful");
    }
    ?>
    <form action="" method="post" enctype="multipart/form-data" name="fjoindre" id="fjoindre">
    Name:
    <input type="text" name="visitor_name">
    Image:
    <input name="visitor_photo" type="file" id="visitor_photo">
    <input type="submit" value="Send" name="Submit">
    </form>
    </body>
    </html>

    but I just need to know what should the type of the field Photo be in the database.

    I have defined the table tb_member as:
    v_ib : int not null
    nom: varchar(40) not null
    photo: longtext not null


    It seems to work but how can I get this image from my database to show it on my pages.

    Thank in advance for all your answers..

  2. #2
    Join Date
    Feb 2004
    Posts
    533
    Most of the programs I've used for image management just store the image name and path in the database and the image is located in a directory on your server. I think it would be slower to extract the image from the database and then display vs. using print or echo to write the path/name into the html output.


    /
    ~

    Bill

  3. #3
    Join Date
    Nov 2004
    Posts
    1
    This is a method I use to show images stored in a MySQL db using PHP

    $type is something like 'image/jpeg'
    $name is just the name of the image
    $image_data is the binary data of the image

    header('Content-type: $type);
    header("Content-Disposition: attachment; filename='$name'");
    header("Content-Description: PHP Generated Data");
    echo $image_data;

    This works fine for me, hope you can use it...

  4. #4
    Join Date
    Nov 2004
    Posts
    1

    Blob

    I believe the type of field is a "blob"

    -G

  5. #5
    Join Date
    Nov 2004
    Posts
    2

    Post

    straight from the mysql docs the types for blobs are

    TINYBLOB
    TINYTEXT
    A BLOB or TEXT column with a maximum length of 255 (2^8 - 1) characters.

    BLOB
    TEXT
    A BLOB or TEXT column with a maximum length of 65535 (2^16 - 1) characters

    MEDIUMBLOB
    MEDIUMTEXT
    A BLOB or TEXT column with a maximum length of 16777215 (2^24 - 1) characters

    LONGBLOB
    LONGTEXT
    A BLOB or TEXT column with a maximum length of 4294967295 or 4G (2^32 - 1) characters






    ---------------------
    dev @
    www.viewmoresoft.com
    tools for your database

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    4

    Not trying to hijack, but this is just what I'm looking for..

    I posted the (almost) exact same question some days ago (just below this post), and I'll try the code you posted Dzilly
    Code:
    $type is something like 'image/jpeg'
    $name is just the name of the image
    $image_data is the binary data of the image
    
    header('Content-type: $type);
    header("Content-Disposition: attachment; filename='$name'");
    header("Content-Description: PHP Generated Data");
    echo $image_data;
    Hope it works.
    Do you output is as : image?id=(id from db) ?

    And if you'll have to output some text as well, how would you alter the code?
    Just add "echo $myTextVariable" ?

Posting Permissions

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