我知道同一主题有几个问题,但没有一个问题能解决我的问题。我尝试过以下问题的方法:
但每种方法都会产生相同的结果。我得到的是这种东西而不是图像: 我的数据库连接代码与插入相同,我可以成功插入图像,只是它们没有显示。我仍然粘贴我的代码,以防它们相关:
连接字符串:
$user = "root";
$pass = "";
$db = "raw_images";
$conn = mysqli_connect('localhost',$user,$pass,$db);
if(!$conn)
{
die(mysqli_error());
}
显示代码:
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';
插入代码:
<!--HTML Form-->
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="files[]" multiple class="Box"/>
<input type="submit" class="Default"/>
</form>
<!--PHP Code-->
$imageData = array();
if(isset($_FILES['files'])){
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_name = $key.$_FILES['files']['name'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
array_push($imageData, $file_name);
$desired_dir="user_data";
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"user_data/".$file_name);
}else{ //rename the file if another one exist
$new_dir="user_data/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
}else{
print_r($errors);
}
if(empty($error)){
$imgDt = implode("|", $imageData);
$query="INSERT INTO tbl_raw_image (image) VALUES('$imgDt'); ";
$rs = mysqli_query($conn, $query);
$imageData = array();
}
}
}
答案 0 :(得分:1)
您忘了采取正确的原始数据。请注意这一行:
$blobimage = addslashes(file_get_contents($_FILES['files']['tmp_name'][$key]));
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$blobimage = addslashes(file_get_contents($_FILES['files']['tmp_name'][$key]));
$filename = $_FILES['files']['name'][$key];
$path = $_FILES['files']['name'][$key];
$ext = pathinfo($path, PATHINFO_EXTENSION);
$desired_dir="upload/";
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("$desired_dir".$filename.'.'.$ext)==false){
move_uploaded_file($_FILES['files']['tmp_name'][$key], $desired_dir.$filename.'.'.$ext);
}else{
$new_dir=$desired_dir.$filename.'.'.$ext.time();
rename($desired_dir.$filename.'.'.$ext,$new_dir) ;
}
}else{
print_r($errors);
}
if(empty($error)){
$query="INSERT INTO tbl_raw_image (image) VALUES('$blobimage'); ";
$rs = mysqli_query($conn, $query);
}
}