Saving Image into mySQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • clarence8816
    New Member
    • Feb 2010
    • 6

    Saving Image into mySQL

    Hi, i have used this code to save my image directly from the web URL to mysql:

    Code:
    $url=http://www.example.com/img.jpg
    $image = addslashes(fread(fopen($url","rb"),$filesize));
    the $image is inserted into mysql under long blob data type. However, when the size of the uploaded image is viewed using mysql, it show a lesser file size than specified. And during retriving, the picture is not complete and is pixilated which i believe is due to the incomplete file size that is saved in mysql, unless I have error in my retriving code.

    Code:
    $row = mysql_fetch_assoc($rs);
    $imagebytes = $row[imgdata];
    header("Content-type: image/jpeg");
    echo $imagebytes;
    It is unable to work for both big and small images
    Any help is appreciated, but I am unable to try it until weekend. Thanks
    Last edited by Atli; Feb 23 '10, 02:58 PM. Reason: Added [code] tags.
  • clarence8816
    New Member
    • Feb 2010
    • 6

    #2
    anyway, using phpmyadmin, i was able to browse to the url and save the pic into the database, and displayed it nicely using the same display codes.

    help anyone?

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hey.

      Check out my article on how to upload files into MySQL. Phase #2 is where the file is uploaded into the database. You would just have to edit it to replace the source of the data; instead of using the $_FILES array to fetch it from a form, fetch the file using file_get_conten ts and determine the require info using filesize and getimagesize.

      Comment

      • clarence8816
        New Member
        • Feb 2010
        • 6

        #4
        thanks for the reply, Atli.
        I understand one of the reason for using file_get_conten t is due to the fact that it is faster.
        I notice under this method's api, i saw this urlencode() and from your article u put real_escape_str ing before inserting into the database.

        To make things mroe understandable, I am using the image search api, so from the result I get from the search, I allow the user to select those images that they want to save into the database. Using the api, I can also retrieve the imagesize and the filesize.

        So is it ok for me to do this? :

        $file=file_get_ contents("http://urlofpic.com/img.jpg",false, 0, $filesizeFromAP I);
        then insert $file into the database blob column

        So do I need to encode using urlencode(), as I will not know what are the urls of the images since it it generated from the search result using the api

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          There is no need to encode the URL using the urlencode function unless you plan to print the URL itself. If you only use it within your PHP code, it makes no difference.

          As to reading the file, this would be enough:
          [code=php]$file=file_get_ contents("http://example.com/img.jpg");[/code]
          The rest of the parameters of the function are optional. By default it reads the entire image as binary data, so there is nothing more you need to set manually.

          If you plan on putting the file data into a MySQL database, the data needs to be escaped to avoid it corrupting the query. That is why we run it through mysql_real_esce ape_string first, to make it safe to use.
          [code=php]$file = mysql_real_esca pe_string($file );
          mysql_query("IN SERT INTO `tbl`(`file`) VALUE('$file')" );[/code]

          An alternative to using mysql_real_esca pe_query is to use prepared (parametrized) statements. That way you let the MySQL driver itself deal with escaping the data.
          [code=php]$mysqli = new mysqli('host', 'user', 'pwd', 'dbName');
          if ($stmt = $mysqli->prepare("INSER T INTO `tbl`(`file`) VALUES(?)"))
          {
          $stmt->bind_param("b" , $file);
          $stmt->execute();
          }[/code]

          Comment

          • clarence8816
            New Member
            • Feb 2010
            • 6

            #6
            thanks, it really worked.

            Comment

            • clarence8816
              New Member
              • Feb 2010
              • 6

              #7
              Filter out forced downloads

              I noticed that if I put in the url which forced downloads, I am unable to save those images into the database.

              One example of the URL i encountered is :



              Any way for me to marked these images down so that I will not save them into the database.
              Or best if there is a work around for me to directly save into the database first using file_get_conten t

              I used Fopen(), and the url is valid. So I'm thinking is it worth to check the header file? But I'm not sure what to look out for.

              Thanks

              Comment

              • clarence8816
                New Member
                • Feb 2010
                • 6

                #8
                Solved

                Thanks anyway, the problem i mentioned is solved.
                The reason it could not be inserted is due to the size, not that it is forced to be downloaded. All I did is just changed the max_allowed_pac ket and its done.

                Comment

                Working...