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
https://forums.phpfreaks.com/topic/111769-solved-mass-image-as-blob-insertion/
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

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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.