为什么有些照片不能上传到数据库?


Why some photos can't upload in database?

在我的应用程序中,我将图像文件存储为数据库中的blob。我考虑让php处理多个图片上传。

脚本似乎工作得很好,但我意识到一些图像文件不能存储在数据库中,所以我改变表图像列的类型从bloblongblob,这一次一些无法上传的照片现在上传正确。

再次

,我意识到仍然有一些图像文件无法存储在数据库中。我花了一些时间来阅读关于blob数据类型和它的长度变化。

经过长时间的调试什么是错的,我从服务器得到这个消息:

在一个事务中插入的BLOB/TEXT数据大小大于重做日志大小的10%。使用innodb_log_file_size增加重做日志大小

请什么可能是我的脚本或数据库的问题?

下面是我的PHP代码和表结构:
--
-- Table structure for table `images`
--
CREATE TABLE `images` (
  `image_id` int(11) NOT NULL,
  `image` longblob NOT NULL,
  `user_id` int(11) DEFAULT NULL,
  `post_id` int(11) DEFAULT NULL,
  `mime_type` varchar(32) DEFAULT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    <?php

if(isset($_FILES['file']['name'])){

        //Handle file upload and status as files description
        //lenght of files
        $files  = array();
        //dataArray
        $dataArray  = array();
        //loop through global FILES variable 
        foreach($_FILES['file']['name'] as $key => $value){
            $files[] = $value;

            $filename   = $_FILES['file']['name'][$key];
            $filesize   = $_FILES['file']['size'][$key];
            $filetemp   = $_FILES['file']['tmp_name'][$key];
            $fileerror  = $_FILES['file']['error'][$key];
            $file_ext   = strtolower(end(explode('.',$filename)));
            $valid_ext  = array('jpg','png','jpeg','gif');
            $maxsize    = 4 *1024 *1024;
            //get file MIME type
            $valid_mime = array('image/jpeg','image/png','image/jpg','image/gif');
            $mime       = getimagesize($filetemp);
            $mime       = $mime['mime'];

            if($filesize > $maxsize || !in_array($file_ext,$valid_ext) || !in_array($mime,$valid_mime) ){
            //tell the user we can't upload files
            $template = "<div class='overlay'></div>
            <div class='er_cnt'>
            <span class='er_cnt_span'>Can't Read Files</span>
            <p> Your photo: <strong>$filename</strong> is not  valid. Photos should be less than 4 MB and saved as JPG, PNG or GIF files. </p>
            <br><hr><button class='close'> Close </button>
            </div>";
            echo $template;
            exit();
            }else{
            //*********Everything is okay so continue*******//
            //resize image...
            //read entire file into a string
            $data   =   file_get_contents($filetemp);

            //append data to the dataArray variable
            $dataArray[] = $data ;
            }

        } //end of first foreach

        //store image in database after foreach loop
        if(count($files) == count($dataArray) ){
        //get the length of dataArray
        $lenght     = count($dataArray);
        //define array to store image id
        $fileID = array();

        //grab the status coming from text area (use as file description)
        //first insert post description into database
        $status     = trim($_POST['status']); 
        $status     = htmlspecialchars($status);
        //prepare and bind statement
                    $sql    = $dbc_conn->prepare("INSERT INTO 
                    $public_feed_table(user_id,post,timepost,file) VALUES(?,?,?,?)");
                    $sql->bind_param('issi',$IsLoggIn,$status,$time_post, $image=1);

                    //execute 
                    $sql->execute();
                    //get the last insert id 
                    $post_id        =    $sql->insert_id;



            //loop through the dataArray
            for($i=0; $i < $lenght; $i++){
            //grab data from the array
            $file_content   =   mysqli_real_escape_string ($dbc_conn,$dataArray[$i]);
            $mime           =   getimagesizefromstring($dataArray[$i]);
            $mime           =   $mime['mime'];

            //save file into database (images table)
            $sql    =   "INSERT INTO $images_table(image,user_id,post_id,mime_type)
             VALUES('$file_content','$IsLoggIn','$post_id','$mime')";

            $query  = mysqli_query($dbc_conn,$sql);
            //append  file id
            $fileID[]   = mysqli_insert_id($dbc_conn); 

        }

        }//end of array equallity



        //store data for use of JSON
            $username       =    getuser($IsLoggIn,'username');
            $post           =    $status;
            $datapost       =    $date_post;
            $total_rating   =    '0';
            $rating_msg     =    'Be the first to rate your tagline';
            $post_id        =    $post_id;
            $post_owner     =    $IsLoggIn;
            $name           =    ucfirst(getuser($IsLoggIn,'firstname'))." ".ucfirst(getuser($IsLoggIn,'lastname'));
            $rate_button    =    true;
            $voters_array   =    array();
            $account_dir    =    getuser($IsLoggIn,'directory');
            $avatar         =    getuser($IsLoggIn,'avatar');
            $file           =    1;
            $pp_id          =   fetch_pp($IsLoggIn);


        //send JSON as server response
        $ajax_data  =   array(
                        "u"         =>  $username,
                        "uid"       =>  $IsLoggIn,
                        "date"      =>  $date_post,
                        "pid"       =>  ''.$post_id.'',
                        "n"         =>  $name,
                        "f"         =>  $file,
                        "ppid"      =>  $pp_id,
                        "imageLength"   =>  $lenght,
                        "fid"           =>  implode(",",$fileID)
                        );
        // retruring data as JSON encode then we use client side to process the data
        echo json_encode($ajax_data);
    }

我从这个答案中解决了这个问题:见这里

如果你正在运行MySQL 5.6.20版本,系统中有一个已知的错误。参见MySQL docs