問題描述
我正在嘗試將圖像文件上傳到我的 MySql 數據庫.我已經在網上搜索過,我終于開始使用以下腳本:
I'm trying to upload an image file to my MySql Database. I've already seacrh on the web and I finally start using the following script:
if(is_uploaded_file($_FILES['filename']['tmp_name'])){
$maxsize=$_POST['MAX_FILE_SIZE'];
$size=$_FILES['filename']['size'];
// getting the image info..
$imgdetails = getimagesize($_FILES['filename']['tmp_name']);
$mime_type = $imgdetails['mime'];
// checking for valid image type
if(($mime_type=='image/jpeg')||($mime_type=='image/gif')||($mime_type=='image/png')){
// checking for size again
if($size<$maxsize){
$filename=$_FILES['filename']['name'];
$imgData =addslashes (file_get_contents($_FILES['filename']['tmp_name']));
if(is_uploaded_file($_FILES['filename']['tmp_name'])){
debug('subida ok');
}else{
debug('fallo subida');
}
//echo $imgData;
$imgDetail=addslashes($imgdetails[3]);
$db = new mysqli('127.0.0.1','user','pass', 'db');
if($db->connect_error){
debug('Error en la conexion : '.$db->connect_errno.
'-'.$db->connect_error);
}
$stmt = $db->stmt_init();
$stmt->prepare("INSERT INTO `image` (`name`, `image`, `type`, `size`) VALUES (?, ?, ?, ?)");
if($stmt===false){ debug('Error en prepare');}
$rc=$stmt->bind_param('sbss', $filename, $imgData, $mime_type,$imgDetail);
if($rc===false){ debug('Error en bind');}
if($stmt->execute()==false){ debug('error ' . $stmt->error); };
$stmt->close();
}else{
debug("<font class='error'>Image to be uploaded is too large..Error uploading the image!!</font>");
}
}else{
debug("<font class='error'>Not a valid image file! Please upload jpeg or gif image.</font>");
}
}else{
switch($_FILES['filename']['error']){
case 0: //no error; possible file attack!
debug("<font class='error'>There was a problem with your upload.</font>");
break;
case 1: //uploaded file exceeds the upload_max_filesize directive in php.ini
debug( "<font class='error'>The file you are trying to upload is too big.</font>");
break;
case 2: //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form
debug( "<font class='error'>The file you are trying to upload is too big.</font>");
break;
case 3: //uploaded file was only partially uploaded
debug( "<font class='error'>The file you are trying upload was only partially uploaded.</font>");
break;
case 4: //no file was uploaded
debug( "<font class='error'>You must select an image for upload.</font>");
break;
default: //a default error, just in case!
debug( "<font class='error'>There was a problem with your upload.</font>");
break;
}
}
上傳的圖像似乎在處理腳本輸出中找到,但查看數據庫大小為 0 字節...有人知道這是為什么嗎?
推薦答案
mysqli SEND_LONG_DATA()!!!
mysqli SEND_LONG_DATA()!!!
http://php.net/manual/en/mysqli-stmt.send-long-data.php
對于某些 php/mysql(尤其是 mysqli 庫)服務器設置,Blob 似乎特別令人沮喪.在配置設置無法解決問題的情況下,此解決方案似乎始終有效.如果重新實施很頭疼,我會建議 PDO.
Blobs seem to be particularly frustrating for certain php/mysql (especially with the mysqli lib) server setups. This solution seems to consistently work where config settings fail to fix the problem. If its a headache to re-implement I would suggest PDO.
這篇關于mysqli 圖像 blob 插入不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!