image store in database long blob
tying to work on upload.php page. where user can upload images. it works fine but when user upload a image size in mb's it give me 3 errors.

Warning: mysql_query() [function.mysql-query]: MySQL server has gone away in C:\xampp\htdocs\a_upload\upload.php on line 69
Warning: mysql_query() [function.mysql-query]: Error reading result set's header in C:\xampp\htdocs\a_upload\upload.php on line 69
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\a_upload\upload.php on line 113

line 69
if(!$insert = mysql_query("INSERT INTO image VALUES(NULL, '$user_id_db', '$image', 
'$image_full_name', '$image_short_name_p', '$image_des_p','$file_size' ,'$image_resolution',0, 0)"))

line 113
$image = mysql_query("SELECT * FROM image WHERE user_id = $id ORDER BY image_id DESC");
                    $image = mysql_fetch_assoc($image); 

full code

<?php
include("include/header.php");

$file = isset($_FILES['fileupload']) ? $_FILES['fileupload']['tmp_name'] : false;
$upload_error = "";

//check, if user is loged in or not
if(isset($_SESSION['username']))      /*** user is loged in ***/
{
    //user get submit button
    if($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        if(empty($_POST['image_short_name']) || empty($_POST['image_des']))
        {
            $upload_error .= "Missing field! Please fill in  all field!";
        }
        else
        {
            $user = $_SESSION['username'];    //get name of who is loged in
            $id = $_SESSION['user_id'];
            $image_short_name_p =  $_POST['image_short_name'];
            $image_des_p       =  $_POST['image_des'];

            //check for existing names
            $query = mysql_query("Select * FROM image WHERE image_short_name = '$image_short_name_p' AND user_id='$id'");       

            if(mysql_num_rows($query) >= 1)
            {
                $upload_error .= "That image name is already taken";
            }
            else
            {
                if($file)
                {   
                    //scussess - store image in $image
                    $image = chunk_split(base64_encode(file_get_contents($_FILES['fileupload']['tmp_name'])));
                    //store image name in $image_full_name(.jpeg)
                    $image_full_name = $_FILES['fileupload']['name'];
                    //store image size in $image_size
                    $image_width_height = getimagesize($_FILES['fileupload']['tmp_name']);
                    $image_resolution = $image_width_height[0] . "X" . $image_width_height[1];  //size of image 100x100
                    $file_size = filesize($file); //get image size, than change to b, kd, mb, or gb
                    if ($file_size < 1024) { $file_size = $filesize . " B"; }
                    else if($file_size < 1048576)  { $file_size = round($file_size / 1024, 2) . " KB"; }
                    else if($file_size < 1073741824) { $file_size = round($file_size / 1048576, 2) . ' MB'; } 
                    echo "$file_size";
                    //test if its a image or file
                    if($image_width_height == FALSE)
                    {
                        $upload_error .= "Thats not a image";
                    }   
                    else
                    { 
                        if(strstr($file_size,'M')) //mb's
                        {
                            $upload_error .= "image is too big";

                            //change size hee


                        } 
                            //get user id and sote in user_id_db(user thats loged in select his rows)
                            $queryget = mysql_query("SELECT user_id FROM user WHERE username = '$user'") or die("query didnt work");
                            $row = mysql_fetch_assoc($queryget);    
                            $user_id_db = $row['user_id'];

                            //insert information into database
                            if(!$insert = mysql_query("INSERT INTO image VALUES(NULL, '$user_id_db', '$image', 
                                '$image_full_name', '$image_short_name_p', '$image_des_p','$file_size' ,'$image_resolution',0, 0)"))
                            {
                                $upload_error .="problem uploading image. please try again";
                            }
                    }
                }
                else
                {
                    $upload_error .= "Select an image!";    
                }
            }
        }        
    }
}

Recommended Answers

All 3 Replies

if(!$insert = mysql_query

is an assignment operation, which will always return true. If you are trying to check the success of the query operation, just use

if (mysql_query(...))

MySQL server has gone away

This means that the connection has been closed or has timed out, and the query is unable to complete execution - likely the cause of your second error.

mysql_fetch_assoc() expects parameter 1 to be resource

I can't see where line 113 is in your page code, but this error usually means that the query has failed. This could be a result of the previous error.

line 133

$image = mysql_fetch_assoc($image); 

i tired changeing to this but still no luck

if(mysql_query("INSERT INTO image VALUES(NULL, '$user_id_db', '$image', 
'$image_full_name', '$image_short_name_p', '$image_des_p','$file_size' ,'$image_resolution',0, 0)"))
{

}
else
{
    $upload_error .="problem uploading image. please try again";
}

it works find if i upload a image size KB.
but when i upload image size in MB. it start giving me this 3 errors.
image type is long blob so it just be fine right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.