Closed Thread
Page 3 of 5 FirstFirst 12345 LastLast
Results 21 to 30 of 48

Thread: Tutorial: Storing Images in MySQL with PHP

  1. #21
    eXcellion is offline Newbie
    Join Date
    May 2009
    Posts
    7
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP

    A question about uploading an image to my database.
    (same question as in other topic, but I need to finish it within 12 hours..)

    I used the tutorial recommended above, but it seems as if I'm to plain dumb to get it to work, I followed the instructions, but it's not working, I get errors about these lines:
    Code:
          $fp fopen($tmpName'r');
          
    $data fread($fpfilesize($tmpName));
          
    $data addslashes($data);
          
    fclose($fp); 
    Warning: fread(): supplied argument is not a valid stream resource in /home/vhosting/21/sintpietersgent.be/www/leerlingenraad/seminarie/insert.php on line 26

    Warning: fclose(): supplied argument is not a valid stream resource in /home/vhosting/21/sintpietersgent.be/www/leerlingenraad/seminarie/insert.php on line 28


    This is the entire code:

    bestellingenCP.php
    HTML Code:
    <table>
    <form enctype="multipart/form-data" action="insert.php" method="post" name="changer">
    <input name="MAX_FILE_SIZE" value="102400" type="hidden">
    <tr><td>Afbeelding:</td><td><input name="image" accept="image/jpeg" type="file"></td></tr>
    <tr><td>Naam:</td><td><input name="naam" type="text" /></td></tr>
    <tr><td>Prijs:</td><td><input name="prijs" type="text" /></td></tr>
    <tr><td>Omschrijving:</td><td><textarea name="omschrijving" ></textarea></td></tr>
    <tr><td></td><td><input value="Submit" type="submit"></td></tr>
    </form>
    </table>
    insert.php (the problem..)

    Code:
    <?php
    $host
    ="localhost";
    $name "correct";
    $pass "correct";
    $dbname "correct";

    $dbi mysql_connect($host$name,$pass) or
    die(
    "Kan niet verbinden met de database. Error :" mysql_error());
    mysql_select_db($dbname,$dbi);

    if (isset(
    $_FILES['image'])) { 


          
    $tmpName  $_FILES['image']['tmp_name'];  
           

          
    $fp fopen($tmpName'r');
          
    $data fread($fpfilesize($tmpName));
          
    $data addslashes($data);
          
    fclose($fp);
          
    $naam $_POST['naam'];
          
    $prijs $_POST['prijs'];
          
    $omschrijving $_POST['omschrijving'];
          

            
    $query "INSERT INTO bestellingen (naam,prijs,beschrijving,image) VALUES
    ('$naam','$prijs','$omschrijving','$data')"

          
    $results mysql_query($query$dbi);
          
    }
    else {
       print 
    "No image selected/uploaded";
    }
    mysql_close($dbi);
    ?>

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #22
    ssSuave is offline Newbie
    Join Date
    Jan 2009
    Posts
    12
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP

    A note referring to my previous post:

    I learned that an upploaded file ( $_FILES['userfile']['tmp_name'] ) has permission 600 and that I have to use chmod() to change permission.

    Do you know where and how i would apply it?


    Thank you in advance for your replies! I really appreciate it

  4. #23
    Jaan Guest

    Re: Tutorial: Storing Images in MySQL with PHP

    Use this function PHP: chmod - Manual when you have uploaded this image.. just after move_uploaded_file() function..

  5. #24
    ssSuave is offline Newbie
    Join Date
    Jan 2009
    Posts
    12
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP

    Thanks a Lot buddy!

  6. #25
    Join Date
    Jul 2009
    Posts
    1
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP

    great tutorial, just what i needed

  7. #26
    arslan220 Guest

    Re: Tutorial: Storing Images in MySQL with PHP

    thanks for this tutorial.

  8. #27
    hardinera's Avatar
    hardinera is offline Learning Programmer
    Join Date
    Jul 2009
    Posts
    42
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP

    thanks for the tutorial really nice hehe

    um.. is it posible that pic_id can hold 2 or more picture at the same time?

    table would be :

    pic_id : int
    pic_1: blob
    pic_2: blob
    pic_3: blob
    ....etc...

    then if i call pic_id all pictures will appear? kinda like that hehe

    or updating the pics?
    just wondering hihihi.. is there a simple/short code for that? >.> like this tutorial?


    thanks,
    hardinera
    Last edited by hardinera; 07-30-2009 at 01:44 AM.

  9. #28
    Filips is offline Newbie
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Please Help : Tutorial: Storing Images in MySQL with PHP

    Helllo Jordon,
    I followed your instruction -

    DB and Table
    DATABASE Name : binary;

    TAble Name : tbl_images (
    > id tinyint(3) unsigned NOT NULL auto_increment,
    > image blob NOT NULL,
    > PRIMARY KEY (id)
    > );

    HTML Code:
    <html>
    <head>
    </head>
    <body>
    <h1>Upload Your Image</h1>
    <form enctype="multipart/form-data" action="insert.php" method="post" name="changer">
    <input name="MAX_FILE_SIZE" value="102400" type="hidden">
    <input name="image" accept="image/jpeg" type="file">
    <input value="Submit" type="submit">
    </body>
    </html>
    insert.php

    Code:
    <html>
    <head></head>
    <body>
    <?php
    // Create MySQL login values and 
    // set them to your login information.
    $username "root";
    $password "1234";
    $host "localhost";
    $database "binary";

    // Make the connect to MySQL or die
    // and display an error.
    $link mysql_connect($host$username$password);
    if (!
    $link) {
        die(
    'Could not connect: ' mysql_error());
    }

    // Select your database
    mysql_select_db ($database);  

    // Make sure the user actually 
    // selected and uploaded a file
    if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

          
    // Temporary file name stored on the server
          
    $tmpName  $_FILES['image']['tmp_name'];  
           
          
    // Read the file 
          
    $fp      fopen($tmpName'r');
          
    $data fread($fpfilesize($tmpName));
          
    $data addslashes($data);
          
    fclose($fp);
          

          
    // Create the query and insert
          // into our database.
          
    $query "INSERT INTO tbl_images ";
          
    $query .= "(image) VALUES ('$data')";
          
    $results mysql_query($query$link);
          
          
    // Print results
          
    print "Thank you, your file has been uploaded.";
    }
    else {
       print 
    "No image selected/uploaded";
    }
    // Close our MySQL Link
    mysql_close($link);
    ?>  
    </body>
    </html>
    End of insert.php

    view.php
    Code:
    <html>
    <head> 
    </head>
    <body>
    <?php

    $username 
    "root";
    $password "1234";
    $host "localhost";
    $database "binary";

    @
    mysql_connect($host$username$password) or die("Can not connect to database: ".mysql_error());

    @
    mysql_select_db($database) or die("Can not select the database: ".mysql_error());

    $id 1;
    //$id = $_GET['id'];

    if(!isset($id) || empty($id)){
    die(
    "Please select your image!");
    }else{

    $query mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
    $row mysql_fetch_array($query);
    $content $row['image'];

    header('Content-type: image/jpg');
    echo 
    $content;
    }
    ?> 
    </body>
    </html>
    end of view.php

    Problem 1: I can add 1 image,even if I tried to insert another image, it shows successfull,but when I open the mysql talbe, I see only one record

    Problem 2: view.php open my Dreamweaver and shows garbage data.

    Can you please help me?
    Last edited by Orjan; 07-31-2009 at 12:38 AM. Reason: Use code tags

  10. #29
    hardinera's Avatar
    hardinera is offline Learning Programmer
    Join Date
    Jul 2009
    Posts
    42
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP

    if i replace insert with update

    $query = "UPDATE members SET image='$data' WHERE id=1";

    but nothing happens >.>

    am i missing sumthing?
    Last edited by hardinera; 07-30-2009 at 09:56 PM.

  11. #30
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: Please Help : Tutorial: Storing Images in MySQL with PHP

    Quote Originally Posted by Filips View Post

    view.php
    Code:
    <html>
    <head> 
    </head>
    <body>
    <?php

    $username 
    "root";
    $password "1234";
    $host "localhost";
    $database "binary";

    @
    mysql_connect($host$username$password) or die("Can not connect to database: ".mysql_error());

    @
    mysql_select_db($database) or die("Can not select the database: ".mysql_error());

    $id 1;
    //$id = $_GET['id'];

    if(!isset($id) || empty($id)){
    die(
    "Please select your image!");
    }else{

    $query mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
    $row mysql_fetch_array($query);
    $content $row['image'];

    header('Content-type: image/jpg');
    echo 
    $content;
    }
    ?> 
    </body>
    </html>
    end of view.php
    The view.php shall NOT have any html tags! Remove them and it will work better to view the images.
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

Closed Thread
Page 3 of 5 FirstFirst 12345 LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. storing images in database
    By ravi951 in forum Database & Database Programming
    Replies: 6
    Last Post: 08-21-2011, 03:11 PM
  2. Replies: 213
    Last Post: 04-14-2011, 07:57 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. storing and load images using linq to sql
    By aglayo2010 in forum C# Programming
    Replies: 0
    Last Post: 09-25-2010, 08:40 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