Tuesday, October 27, 2009

Get diffrent elements in two arrays

How to get the diffrent elements in two arrays in PHP?


You can get the array elements which are not common to both arrays.
use the following php code.
<?php


$ar1=array("A","B","C");
$ar2=array("A","B");


$diffrence=array_diff($ar1,$ar2);


print_r($diffrence);




?>

out put:
Array ( [2] => C )
C is the item which is only common to the $ar1. Likewise array_diff returns an array of elements not common to the both  arrays.

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