在我的应用程序中,我将图像文件作为blob存储在数据库中。我考虑让php处理多个图片上传。
脚本似乎工作得很好,但我发现一些图像文件无法存储在数据库中,因此我将表格图像列的类型从 blob 更改为 longblob 现在可以正确上传其中一些无法上传的照片。
再次,我意识到仍有一些图像文件仍然无法存储在数据库中。 我花时间阅读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);
}