Monday, October 5, 2009

How to upload and encrypt a file in PHP

How to upload a file and encrypt in PHP?

Following example shows you how to upload and encrypt a file using PHP.
In some cases you may want to upload a file to server and encrypt it. You may also want to encrypt the file name also. Using PHP you can do this easily. In this example md5 hash function is used to encrypt the file name of the uploaded file. base64_encode encryption is used to encrypt the content of the uploaded file. "uploadDirectory" is the folder which is used to save the encrypted file after upload to the server. The encrypted file save under ".data" extension. You can user any extension for save the file. When you need to decrypt the file, you can user base64_decode to get the actual file. Also you can keep original file name and encrypted file name in a data base and use them to decrypt the file when you need it.

1. Example code upload and encrypt a file in PHP.

PHP Code
<?php //code for save_file.php (form action)
$uploadDirectory = "images/"; //folder to save the encrypted file $fileName = $_FILES['fileToUpload']['name']; $tempFileName = $_FILES['fileToUpload']['tmp_name'];
$error = $_FILES['fileToUpload']['error']; $fileContentType = $_FILES['fileToUpload']['type']; $fileSize = $_FILES['fileToUpload']['size'];
if($error==UPLOAD_ERR_OK){
$file = fopen($tempFileName,"r"); $content = fread($file,filesize($tempFileName)); $encryptedContent = base64_encode($content);
$encryptedFileSaveName=$uploadDirectory.md5($insert_id).".data";
$encryptedFile = fopen($encryptedFileSaveName,'w'); fwrite($encryptedFile,$encryptedContent); fclose($encryptedFile); print("File has been upload and encrypted successfully.");
}else{ print("Error while uploading......"); }
?>
Html Code
<html> <head> <title>How to encrypt and upload file using PHP </title> </head> <body> <form action="save_file.php" method="post" enctype="multipart/form-data" name="formFileUpload" id="formFileUpload"> <table border="0"> <tr> <td>Select a file </td> <td><input name="fileToUpload" type="file" id="fileToUpload"></td> </tr> <tr> <td>&nbsp;</td> <td> <input name="btnUploadFile" type="submit" id="btnUploadFile" value="Upload"> </td> </tr> </table> </form> </body> </html>
Bookmark and Share
» How to create random validation image in PHP
» How to get extension of a file using PHP
» How to upload and encrypt file in PHP
» How to upload files in PHP
» How to create arrays in PHP
» How to create thumbnail images in PHP
» How to connect MySQL Database in PHP
©-Copyright By Duminda Chamara JavaScript Validation

4 comments:

My Project said...

hye, i'm now build a final year project that needs to encrypt a file using AES algorithm. The system will prompt for user password first before the file can be encrypt. Can you help me?

Anonymous said...

Base 64 is NOT encryption, it is simply a way to encode a file. If somebody got hold of the file contents they could easily use the php base64_decode() function to get the original file back.

Jaga said...

How to decrypt encrypted php file using php

Unknown said...

for the same example tell how to decrypt the same encrypted file done by ur example...