如何在PHP中基于64加密和基础64解密图像文件?

时间:2016-06-10 06:44:33

标签: php php-5.5

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



<?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)) {
	// FILE TYPE CHECKING
	 $allowed =  array('gif','png' ,'jpg');
	if(!in_array($extension,$allowed) ) {
		$horoscope = array("message" => "filetype error");
		echo json_encode($horoscope);
	}
	else{
	if($_FILES['file']['size']>4459681){
		$horoscope = array("message" => "filesize error");
		echo json_encode($horoscope);
	}
	else{
		//echo "filesize(success)";
		$sql=mysql_query("SELECT id_proof FROM user_details WHERE user_id='$userid'");
		$count = mysql_num_rows($sql);
		if($count != 0){
			$filemove = array("filemove" => move_uploaded_file($_FILES['file']['tmp_name'], "horoscope/".$new_name));
			$sql=mysql_query("UPDATE user_details SET id_proof='$new_name',updated_on='$updated_on' WHERE user_id='$userid'");
			if($sql){
			$horoscope = array("message" => "update success");
			}else{
			$horoscope = array("message" => "update error");
			}
			echo json_encode($horoscope);
		}else{
			$filemove = array("filemove" => move_uploaded_file($_FILES['file']['tmp_name'], "horoscope/".$new_name));
			$sql = mysql_query("INSERT INTO user_details (user_id,horoscope,approved,approved_on,status) VALUES ('$userid','$new_name','$approved','$approved_on','$status')");
			if($sql){
			$horoscope = array("message" => "successully insert");
			}else{
			$horoscope = array("message" => "insert error");
			}
			echo json_encode($horoscope);
		}
	} 
	} 
}else{
	//echo "Error";
	$horoscope = array("message" => "file is not moving");
	echo json_encode($horoscope);
}	
?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:-1)

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

&LT; ----- -----编码GT&;

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

可以存储到数据库图像数据 - &gt; $ IMDATA
&LT; ----- /编码-----&GT;

&LT; ----- -----解码&GT;

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

&LT; ----- /解码-----&GT;