問題描述
我必須將輸入字段中的文件保存到數(shù)據(jù)庫中,我會這樣做:
i have to save the files from input fields to database,i'll do it this way:
$mkey = mysqli_insert_id($mysqli);
$i=0;
while (isset($_FILES['file'.$i])) {
$filepath = ini_get('upload_tmp_dir')."/".basename($_FILES['file'.$i]['tmp_name']);
$filepath = addslashes($filepath);
$handle = fopen($filepath, "rb");
$content = fread($handle, filesize($filepath));
$stmt = $mysqli->prepare("INSERT INTO attachment (filename,filecontent,mkey) VALUES (?,?,?)");
$stmt->bind_param("sbi",$_FILES['file'.$i]['name'],$content,$mkey);
$stmt->execute();
fclose($handle);
$i++;
}
沒有發(fā)生錯(cuò)誤,其他 2 個(gè)字段 mkey 和 filename 填充在數(shù)據(jù)庫列中,但內(nèi)容沒有,$content 在我的瀏覽器中顯示為 print_r,我確定 $content 變量不為空,但 mysql 沒有顯示任何內(nèi)容內(nèi)容.
no error occured,and the other 2 fields mkey and filename are filled in database columns,but the content no,$content shown with print_r in my browser and i sure the $content variable is not empty,but mysql shows me nothing of the content.
推薦答案
RTM ;-)
如果變量的數(shù)據(jù)大小超過最大值.允許的數(shù)據(jù)包大小(max_allowed_pa??cket),您必須在類型中指定 b 并使用 mysqli_stmt_send_long_data() 以包的形式發(fā)送數(shù)據(jù).
If data size of a variable exceeds max. allowed packet size (max_allowed_packet), you have to specify b in types and use mysqli_stmt_send_long_data() to send the data in packets.
所以我自己從來沒有這樣做過,但我認(rèn)為它需要根據(jù)您的代碼和 函數(shù)文檔頁面上的示例:
So I have never done this myself but i would assume it needs to look something like this based on your code and the example on the functions doc page:
$filepath = ini_get('upload_tmp_dir')."/".basename($_FILES['file'.$i]['tmp_name']);
$filepath = addslashes($filepath);
$handle = fopen($filepath, "rb");
$content = null;
$stmt = $mysqli->prepare("INSERT INTO attachment (filename,filecontent,mkey) VALUES (?,?,?)");
$stmt->bind_param("sbi",$_FILES['file'.$i]['name'], $content, $mkey);
while (!feof($handle)) {
// $maxPacketSize would be the size of your max packet setting for mysql,
// or something safely assumed to be below it
$stmt->send_long_data(1, fread($handle, $maxPacketSize));
}
fclose($handle);
$stmt->execute();
這篇關(guān)于php print_r 中顯示的二進(jìn)制文件內(nèi)容但未保存在 mysql 中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!