www.webdeveloper.com
Results 1 to 4 of 4

Thread: Picture File Upload Database PHP

  1. #1
    Join Date
    Apr 2012
    Posts
    15

    Post Picture File Upload Database PHP

    I am trying to add a picture upload for a project that i am currently doing, but whenever i try this it does not display correctly on the page. But if i add the picture using phpMyadmin it displays fine on the webpage. To display the image on the page i am using this code.
    HTML Code:
    <img src="data:image/jpeg;base64,'. base64_encode( $row['review_picture'] ). '" style="height: 150px; width: 150px;"  />
    For the upload i am working on, it involves a form, with other inputs beside the file upload, but the code below, is in its basic form

    HTML Code:
    <form action="new_review.php" method="post" name="myForm">
    <label>Review Picture: </label><input type="file" name="review_picture" id="review_picture">
    </form>
    But the part which i am really struggling on is the page, which it is posted to.

    PHP Code:
    <?php 
    Open_Connection
    (); 
        
        global 
    $connection_Var;
        
    $data file_get_contents('$_POST[review_picture]');
        
    $data base64_encode($data);
        
    mysqli_query($connection_Var,"INSERT INTO review(review_picture) VALUES ('$base64')  or die (mysql_error());

        header( 'Location: ../index.php');
        Close_Connection();
        
        
        function Open_Connection()



    {
        global 
    $connection_Var;
        
    $connection_Var = mysqli_connect("localhost","root","","review_db"); 
        if (mysqli_connect_errno(
    $connection_Var))
          {
          echo "
    Failed to connect to MySQL" . mysqli_connect_error();
          }
    }
    function Close_Connection()
    {
        global 
    $connection_Var;
        mysqli_close(
    $connection_Var); 
    }
    ?>
    I have tried many different ways round this, what ever i try i can't quite get the hang of it, and tbf it the last piece of code is a bit of rough guess. I would be really grateful for any help.

  2. #2
    Join Date
    Apr 2012
    Posts
    15
    So what i'm thinking at the moment is that once the image has been posted to the php page, it then needs to be converted to the base64, which i think is correct, because i use base64 to display a image uploaded directly to the database. And then the base64 code, should then be added to to "INSERT INTO" etc, right? But i just don't actually know how i would actually do this.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,337
    My first suggestion would be to not store the actual image in the DB, since it's just a binary file that you are never going to search or sort on. Instead, store in the file system (since it is, in fact, just a file), perhaps generating some sort of unique file name for it (see uniqid(), etc.), then store information about the image (including its file name) in the database. Then your image-serving script just needs to query the DB to get the applicable file name, send an appropriate content-type header() (probably also retrieved from the DB), then do a readfile() on the actual image file.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    May 2013
    Posts
    119
    Nice topic, its really helpful for me, thanks to all of you for sharing your ideas and feedback.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

Posting Permissions

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



Recent Articles