Jump to content

Images in DB, it's doing my head in!


ll87

Recommended Posts

Hey guys, first off I am new to PHP and everything im doing is new to me. Ive read all the arguments for and against storing Images as BLOBs in a MySQL database, but i have to and i won't be having that many images anyways.

 

I have a script (constructed after looking at many examples) but it appears nothing is uploading, so surely it must be something wrong with my queries?

 

If i just run it with echo'ing the $_FILES data it displays that correctly so it's a problem with my queries.

 

Here's my upload code:

 

<?php
   session_start();

   include 'database.php'; //connects, works fine.

   $id = $_SESSION['id']; //user id.

   echo $fileName = $_FILES['image_upload']['name'];
   $fileType = mysql_real_escape_string($_FILES['image_upload']['type']);
   $fileSize = round($_FILES['image_upload']['size'] / 1024); //store size in KB.
   $fileTempPath = mysql_real_escape_string($_FILES['image_upload']['tmp_name']);

   /* Make sure image is of the type gif or jpeg and no more than 250KB */
   if($fileType == 'image/gif' || $fileType == 'image/jpeg' && $fileSize < 250){
       if($_FILES['file']['error'] > 0){
           echo 'Error: ' . $_FILES['image_upload']['error'];
       }
       else{
           $fp = fopen($fileTempPath,'r'); //open temporary file.
           $content = fread($fp,filesize($fileTempPath)); //start at start of file, read until end of file.
           fclose($fp);

           /* check to see if user already has an image */
           $checkForImage = mysql_query("SELECT member_id FROM images WHERE member_id = '$id'") or die(mysql_error());
           if(mysql_num_rows($checkForImage) != 0){
               $updateQuery = "UPDATE images SET img_name='$fileName',img_type='$fileType',img_size='$fileSize',img='$content' WHERE member_id='$id'";
               $updateImage = mysql_query($updateQuery);
           }
           else{
               /* else insert new image with id of the user stored in member_id column in images table */
               $insertQuery = "INSERT INTO images(img_name,img_type,img_size,img,member_id) VALUES('$fileName','$fileType','$fileSize','$content','$id')" or die('something wrong?');
               $insertImage = mysql_query($insertQuery);

           }  
       }
   }



?>

 

i have read tons of forums and this is my first time posting to find some help, so any would be great!

 

Thanks

Edited by ll87
Link to comment
Share on other sites

if im understanding you correctly, you mean take off the conditionals and just leave:

 

$insertQuery = "INSERT INTO images(img_name,img_type,img_size,img,member_id) VALUES('$fileName','$fileType','$fileSize','$content','$id')" or die('something wrong?');
               $insertImage = mysql_query($insertQuery);

 

this still uploads nothing

Link to comment
Share on other sites

well i started without the real escape string but ive tested your code with it there and with it not there. visibily nothing altered:

 

and i took the round() off for now.

 


Array
(
   [image_upload] => Array
       (
           [name] => vIWDkn2wMI.gif
           [type] => image/gif
           [tmp_name] => /private/var/tmp/php932pd0
           [error] => 0
           [size] => 1563
       )

)

vIWDkn2wMI.gifimage/gif1563/private/var/tmp/php932pd0

 

nothing unexpected there right?

Edited by ll87
Link to comment
Share on other sites

ok so i found that i needed to use MEDIUMBLOB instead of BLOB for my 'img' field in my database as MEDIUMBLOB holds more. i thought 'bingo!' but no joy, nothing is being inserted at all.

 

and i'm not getting any error messages to work from, does it think it is working?

 

im using mysql query browser to check whether there's any data going in, and nothing

Link to comment
Share on other sites

well i was correct to use MEDIUMBLOB as it can hold upto 16MB i think, where BLOB didnt hold enough, so that was the first problem.

 

i was mistaken when i thought that hadn't fixed it, i was simply trying to upload a file too large and hadnt set up error checking adequately enough.

 

now i have checks for the image dimensions as well.

 

thanks for the help, that print_r is quite handy.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...