1)确保图像足够小或存储位置足够大。如果你有超过65kB的东西,你需要一个longblob而不是blob。超过这个尺寸的任何东西都会被截断并丢失。
2)在插入到数据库中之前,不要在加密之前移动addslashes。单引号(或者取决于你使用的是双引号)将字符串的开始和结尾指定给MySQL引擎。 addslashes函数转义这些和其他特殊字符,以防止他们混淆他们的MySQL引擎。事实上,它在加密之前执行它而将记录添加到数据库中仅仅是偶然的机会。
3)您应该知道这些图像正在作为临时文件保存在服务器上。除非采取特别的预防措施,否则其中的数据将保留在HDD的松弛空间中。它可以很容易地被对手使用取证或修复工具进行检索。
<html>
<head><title>Picture</title></head>
<body>
<form enctype="multipart/form-data" action="file.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="600000" />
<input type="file" name="users_image"/>
<input type="submit" text="Upload">
</form>
<?
if($_SERVER['REQUEST_METHOD'] === 'POST')
{
$image = file_get_contents($_FILES['users_image']['tmp_name']);
//encrypt
$cipher = "aes-128-cbc";
$ivlen = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes($ivlen);
$key = openssl_random_pseudo_bytes(128);
$ciphertext = openssl_encrypt($image, $cipher, $key, $options=0, $iv);
//add to DB
$mysqli = mysqli_connect("localhost","testu","","test");
$query = "INSERT INTO blobtbl(pics) VALUES (\"" . addslashes($ciphertext) ."\")";
$mysqli->query($query);
$id = mysqli_insert_id($mysqli);
//retireve from DB
$sql = "SELECT * FROM blobtbl WHERE id = $id";
$res = $mysqli->query($sql);
$row=mysqli_fetch_assoc($res);
$newciphertext = $row['pics'];
//decrpyt and display
$img = openssl_decrypt($newciphertext, $cipher, $key, $options=0, $iv);
echo '<img src="data:image/jpeg;base64,'.base64_encode($img).'"/>';
echo "<br>Did it work?";
}
?>
</body>
</html>
当我删除它甚至没有正确插入数据库。 – Bing
什么是Apache错误日志或BD错误日志?你的加密库或方法是什么? –