如何基地64加密和基地64解密PHP中的图像文件?

问题描述:

我有一个名为文件上传的表单域,在这里我移动了一个tmp_folder,之后我存储在数据库中,从这里工作正常,但我的要求是我不想在tmp_folder中移动,我想要加密文件名并直接插入数据库,我想我想使用base64加密,但我不知道如何使用?如何基地64加密和基地64解密PHP中的图像文件?

<?php 
 
$userid = $_POST['userid'];// here i got userid 
 
$filename = basename($_FILES['file']['name']);//here i got filename 
 
$extension = pathinfo($filename, PATHINFO_EXTENSION); 
 
$new_name= md5($filename.time()).'.'.$extension; 
 
$approved = 0; 
 
$approved_on = date('Y-m-d H:i:s'); 
 
$updated_on = date('Y-m-d H:i:s'); 
 
$status = 0; 
 
if (!empty($new_name)) { 
 
\t // FILE TYPE CHECKING 
 
\t $allowed = array('gif','png' ,'jpg'); 
 
\t if(!in_array($extension,$allowed)) { 
 
\t \t $horoscope = array("message" => "filetype error"); 
 
\t \t echo json_encode($horoscope); 
 
\t } 
 
\t else{ 
 
\t if($_FILES['file']['size']>4459681){ 
 
\t \t $horoscope = array("message" => "filesize error"); 
 
\t \t echo json_encode($horoscope); 
 
\t } 
 
\t else{ 
 
\t \t //echo "filesize(success)"; 
 
\t \t $sql=mysql_query("SELECT id_proof FROM user_details WHERE user_id='$userid'"); 
 
\t \t $count = mysql_num_rows($sql); 
 
\t \t if($count != 0){ 
 
\t \t \t $filemove = array("filemove" => move_uploaded_file($_FILES['file']['tmp_name'], "horoscope/".$new_name)); 
 
\t \t \t $sql=mysql_query("UPDATE user_details SET id_proof='$new_name',updated_on='$updated_on' WHERE user_id='$userid'"); 
 
\t \t \t if($sql){ 
 
\t \t \t $horoscope = array("message" => "update success"); 
 
\t \t \t }else{ 
 
\t \t \t $horoscope = array("message" => "update error"); 
 
\t \t \t } 
 
\t \t \t echo json_encode($horoscope); 
 
\t \t }else{ 
 
\t \t \t $filemove = array("filemove" => move_uploaded_file($_FILES['file']['tmp_name'], "horoscope/".$new_name)); 
 
\t \t \t $sql = mysql_query("INSERT INTO user_details (user_id,horoscope,approved,approved_on,status) VALUES ('$userid','$new_name','$approved','$approved_on','$status')"); 
 
\t \t \t if($sql){ 
 
\t \t \t $horoscope = array("message" => "successully insert"); 
 
\t \t \t }else{ 
 
\t \t \t $horoscope = array("message" => "insert error"); 
 
\t \t \t } 
 
\t \t \t echo json_encode($horoscope); 
 
\t \t } 
 
\t } 
 
\t } 
 
}else{ 
 
\t //echo "Error"; 
 
\t $horoscope = array("message" => "file is not moving"); 
 
\t echo json_encode($horoscope); 
 
} \t 
 
?>

+1

的Base64编码*** ***不*”加密”*! – deceze

+0

K怎么能做到我的要求? –

+0

我不清楚这个要求究竟是什么。你只是想*将文件保存到数据库*?你不需要base64。只需'file_get_contents'来读取文件,将其放入您的查询中以保存到BLOB列中,这就是它。 – deceze

$filename = $_FILES['file']['image'] 
$destinationPath = 'upload path'; 
move_uploaded_file($destinationPath, $fileName); 

< ----- -----编码>

$im = file_get_contents(file path with filename); 
$imdata = base64_encode($im); 

可以存储到数据库的图像数据 - > $ IMDATA
< - ---- /编码----->

< ----- -----解码>

$ifp = fopen($output_file_path_with_filename, "wb"); 
$$imdata = $base64_string; // $base64_string; from your database. 
fwrite($ifp, base64_decode($imdata)); 
fclose($ifp); 

< ----- /解码----->

+0

$ path = basename($ _ FILES ['file'] ['name']); //在这里我得到文件名 –

+0

file_get_contents($ path); //这里我越来越false.so我不能编码数据 –

+0

第一个存储文件使用php 之后,给出文件名与你的路径 –