我必须加密文件并将其作为blob保存在mysql中,然后解密并使其可供下载。 我将文件保存在blob中,如下所示:
$certificate_tmp=$_FILES['certificate']['tmp_name'];
$certificate=openssl_encrypt(file_get_contents($certificate_tmp),$ciphers,$password_tmp);
$wpdb->insert("my_table",array("certificate"=>$certificate));
注意:我已经删除了不相关的代码,该表不仅仅是证书,但我不希望这会让人感到困惑。
这是下载php页面:
$password_tmp=$_SESSION['pwd']; //decrypt password
global $wpdb; //wordpress db conncetion
$results_file = $wpdb->get_row("select * from my_table where id='$id'",ARRAY_A); //i get the id from wp's curr_user
$m_file = openssl_decrypt($results_file['certificate'],"AES-128-CBC",$password_tmp);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\certificate".$id."\"");
header('Content-Transfer-Encoding: binary');
print_r($m_file);
一切都与文本文件完美配合,但结果是二进制文件为空,尽管在blob中二进制文件存在。
修改
我的猜测是,只要我从db blob解密文件,php或html(因为print_r
)就知道它是一个二进制文件,并且由于安全原因不允许你显示它。您无法在网络上执行程序,如 .exe或.bin ,尽管我上传的文件(二进制或文本)没有扩展名。
根据我的理解,php将二进制文件视为字符串,但file_get_contents
是二进制安全。
我认为不使用blob将是最好的方法,但我不能这样做,我必须使用blob。
答案 0 :(得分:1)
为什么使用print
代替print_r
?
尝试添加内容长度:
标题('Content-Length:'。strlen(YourFileData));
欲了解更多信息,请访问: http://www.media-division.com/the-right-way-to-handle-file-downloads-in-php/