php對(duì)文件進(jìn)行hash運(yùn)算的方法
本文實(shí)例講述了php對(duì)文件進(jìn)行hash運(yùn)算的方法。分享給大家供大家參考。具體如下:
這段代碼非常有用,如果你下載了一個(gè)文件,網(wǎng)站提供了hash結(jié)果,你可以對(duì)你下載下來(lái)的文件進(jìn)行hash運(yùn)算,以驗(yàn)證下載的文件是否正確。
<html>
<head>
<title>Hash (Check) Files</title>
<style type='text/css'>
#ok{color:green;}
#nono{color:red;}
</style>
</head>
<body>
<?php
if(!empty($_FILES)){
if ($_FILES["file"]["error"] > 0){
switch($_FILES["file"]["error"]){
case 1:
echo "<b id='nono'>Error: The uploaded file exceeds the upload_max_filesize directive in php.ini</b><br>";
break;
case 2:
echo "<b id='nono'>Error: The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.</b><br>";
break;
case 3:
echo "<b id='nono'>Error: The uploaded file was only partially uploaded.</b><br>";
break;
case 4:
echo "<b id='nono'>Error: No file was uploaded.</b><br>";
break;
case 6:
echo "<b id='nono'>Error: Missing a temporary folder.</b><br>";
break;
case 7:
echo "<b id='nono'>Error: Failed to write file to disk.</b><br>";
break;
case 8:
echo "<b id='nono'>Error: A PHP extension stopped the file upload.</b><br>";
break;
default:
echo "<b id='nono'>Unknown error occured.</b><br>";
}
} else {
echo 'Upload: ' . $_FILES['file']['name'] . '<br>';
echo 'Type: ' . $_FILES['file']['type'] . '<br>';
echo 'Size: ' . (round($_FILES['file']['size'] / 1024, 2)) . ' Kb<br><br>';
if(array_search($_POST['algo'], hash_algos())===false){
echo 'Unknown hashing algorithm requested.<br>';
} else {
echo 'Hashing Algorithm: '. $_POST['algo'] . '<br>';
$hash = hash_file($_POST['algo'], $_FILES['file']['tmp_name']);
echo 'Calculated hash: ' . $hash . '<br>';
if($_POST['exphash']!=='none' && !empty($_POST['exphash'])){
echo 'Expected hash: ' . $_POST['exphash'] . '<br><br>';
echo ($hash==$_POST['exphash'])? '<b id="ok">Hash matched expected value.</b>' : '<b id="nono">Hash did not match expected value.</b>';
echo '<br>';
}
}
}
?>
<br>
<button onClick="document.location.reload(true)">Again</button>
<?php
} else {
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="exphash" value="none">
<label for="file">Filename:</label>
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit" /><br>
<label>Expected hash(optional): <input type="text" name="exphash" size="100"></label>
<br><br>Choose an algorithm (This is the list of all the available algorithms in your php installation)<br>
<?php
foreach(hash_algos() as $algo){
if($algo=='md5'){
echo "<label><input type='radio' name='algo' value='$algo' checked='checked'>$algo</label><br>";
} else {
echo "<label><input type='radio' name='algo' value='$algo'>$algo</label><br>";
}
}
?>
</form>
<?php
}
?>
</body>
</html>
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
相關(guān)文章
mcrypt啟用 加密以及解密過(guò)程詳細(xì)解析
Mcrypt庫(kù)支持20多種加密算法和8種加密模式,具體可以通過(guò)函數(shù)mcrypt_list_algorithms()和mcrypt_list_modes()來(lái)顯示2013-08-08
Android ProgressBar進(jìn)度條和ProgressDialog進(jìn)度框的展示DEMO
本篇文章是對(duì)Android中ProgressBar進(jìn)度條和ProgressDialog進(jìn)度框的展示DEMO進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Windows Server 2008 R2和2012中PHP連接MySQL過(guò)慢的解決方法
這篇文章主要介紹了Windows Server 2008 R2和2012中PHP連接MySQL過(guò)慢的解決方法,同時(shí)對(duì)Windows 7和8的本地開發(fā)環(huán)境也有效,需要的朋友可以參考下2016-07-07
PHP設(shè)計(jì)模式之組合模式定義與應(yīng)用示例
這篇文章主要介紹了PHP設(shè)計(jì)模式之組合模式定義與應(yīng)用,結(jié)合實(shí)例形式詳細(xì)分析了PHP組合模式基本原理、定義與使用方法,需要的朋友可以參考下2020-02-02
JS+PHP實(shí)現(xiàn)用戶輸入數(shù)字后顯示最大的值及所在位置
這篇文章主要給大家介紹了JS+PHP實(shí)現(xiàn)用戶輸入數(shù)字后顯示最大的值及是第幾個(gè)的相關(guān)位置,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。2017-06-06

