Jump to content

[SOLVED] Mass image as blob insertion


webent

Recommended Posts

Could someone please take a look at my code and tell me what I am doing wrong? What I'm trying to do is insert all images, 50 at a time, there are more than 200,000, that are in a directory into a mysql database... Here's my code thus far...

 

require "connection.php";
$dirname = "/home/username/public_html/storage/images/";
$new_destination = "/home/username/public_html/storage/inserted_images/";
$pattern = "(\.jpg$)|(\.JPG$)|(\.jpeg$)|(\.JPEG$)|(\.png$)|(\.PNG$)";
$files = array();
if (isset($_POST['insert_images'])) {
    if ($handle = opendir($dirname)) {  
        while(false !== ($files = readdir($handle)))	{ 
            $complete_image_path = $dirname . $files;
            $file_size = filesize($complete_image_path);          
            list(,,$pattern) = getimagesize($complete_image_path);
            $file_type = image_type_to_extension($pattern);
            $file_name = $files;
####################################################################
if ($file_type != "") {
$insert_counter = 0;    
while ($insert_counter < 51) {
    $insert_counter++;
// Do Insert 50 times
    $fp = fopen($complete_image_path, "rb");
    $content = fread($fp, $file_size);
    $content = addslashes($content);
    $query = "INSERT INTO images VALUES ('$file_name', '$file_size', '$file_type', '$content')";
        $result = mysql_query($query);
        if (!$results) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
        die($message);
        }
    // Move the image that was just inserted           
    if(!move_uploaded_file($file_name, $new_destination)) { 
        die; 
    }
    fclose($fp);
}
}
####################################################################                                                    
        }   
    }
    closedir($handle);
}

 

Here's a clip of the error, I cut it off, or it would go on for miles...

 

Invalid query: Whole query: INSERT INTO images VALUES ('739-736-444736.jpg', '80232', '.jpeg', 'ÿØÿà\0JFIF\0\0\0d\0d\0\0ÿì\0Ducky\0\0\0\0\0<\0\0ÿî\0Adobe\0dÀ\0\0\0ÿÛ\0„\0       ÿÀ\0&&\0ÿÄ\0¿\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0!1A\"Qaq‘2R#¡±BÁÑbrS‚’¢3C$5ðá²%ñÒc4DÂTâsƒ£\0\0\0\0!1AQaq\"ð‘¡2R±ÁÑáB¢#ñb’3Scr‚C²ÒâòÿÚ\0 \0\0?\0ú¦€P @(\0 €P @(\0 €P @(\0 €P @(\0 €P @(\0 €P @(\0 €P @(\0 €P @(\0 €P @(\0 €P @(\0 €P @(\0 €P @(\0 €P @(\0 €P @(\0 €P @(\0 €P @(

Link to comment
Share on other sites

Typically I see an INSERT commands that first list the field names and then the values. Your query is missing the field names:

 

INSERT
INTO images (field1, field2, field3, field4)
VALUES ('739-736-444736.jpg', '80232', '.jpeg', 'ÿØÿà\0JFIF\0\0\0d\0d\0\0ÿì\0Ducky\0\0\0\0\0<\0\0ÿî\0Adobe\0dÀ\0\0\0ÿÛ\0„\0

Link to comment
Share on other sites

I've never done them that way, I've done them for the last 9 years this way, I don't think it makes a difference...

 

FYI, I thought maybe the blob was perhaps too big for the mysql mediumblob, so I changed it to longblob, it didn't make a difference...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.