国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php對文件進行hash運算的方法

2024-05-04 21:56:02
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了php對文件進行hash運算的方法,涉及針對文件的hash運算技巧,非常具有實用價值,需要的朋友可以參考下

本文實例講述了php對文件進行hash運算的方法。分享給大家供大家參考。具體如下:

這段代碼非常有用,如果你下載了一個文件,網站提供了hash結果,你可以對你下載下來的文件進行hash運算,以驗證下載的文件是否正確。

  1. <html> 
  2. <head> 
  3.   <title>Hash (Check) Files</title> 
  4.   <style type='text/css'
  5.    #ok{color:green;} 
  6.    #nono{color:red;} 
  7.   </style> 
  8. </head> 
  9. <body> 
  10.  <?php 
  11.    if(!emptyempty($_FILES)){ 
  12.    if ($_FILES["file"]["error"] > 0){ 
  13.     switch($_FILES["file"]["error"]){ 
  14.      case 1: 
  15.      echo "<b id='nono'>Error: The uploaded file exceeds the upload_max_filesize directive in php.ini</b><br>"
  16.      break
  17.      case 2: 
  18.      echo "<b id='nono'>Error: The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.</b><br>"
  19.      break
  20.      case 3: 
  21.      echo "<b id='nono'>Error: The uploaded file was only partially uploaded.</b><br>"
  22.      break
  23.      case 4: 
  24.      echo "<b id='nono'>Error: No file was uploaded.</b><br>"
  25.      break
  26.      case 6: 
  27.      echo "<b id='nono'>Error: Missing a temporary folder.</b><br>"
  28.      break
  29.      case 7: 
  30.      echo "<b id='nono'>Error: Failed to write file to disk.</b><br>"
  31.      break
  32.      case 8: 
  33.      echo "<b id='nono'>Error: A PHP extension stopped the file upload.</b><br>"
  34.      break
  35.      default
  36.      echo "<b id='nono'>Unknown error occured.</b><br>"
  37.     } 
  38.    } else { 
  39.     echo 'Upload: ' . $_FILES['file']['name'] . '<br>'
  40.     echo 'Type: ' . $_FILES['file']['type'] . '<br>'
  41.     echo 'Size: ' . (round($_FILES['file']['size'] / 1024, 2)) . ' Kb<br><br>'
  42.     if(array_search($_POST['algo'], hash_algos())===false){ 
  43.     echo 'Unknown hashing algorithm requested.<br>'
  44.     } else { 
  45.     echo 'Hashing Algorithm: '$_POST['algo'] . '<br>'
  46.     $hash = hash_file($_POST['algo'], $_FILES['file']['tmp_name']); 
  47.     echo 'Calculated hash: ' . $hash . '<br>'
  48.     if($_POST['exphash']!=='none' && !emptyempty($_POST['exphash'])){ 
  49.       echo 'Expected hash:   ' . $_POST['exphash'] . '<br><br>'
  50.       echo ($hash==$_POST['exphash'])? '<b id="ok">Hash matched expected value.</b>' : '<b id="nono">Hash did not match expected value.</b>'
  51.       echo '<br>'
  52.     } 
  53.     } 
  54.    } 
  55.    ?> 
  56.    <br> 
  57.    <button onClick="document.location.reload(true)">Again</button> 
  58.     <?php 
  59.    } else { 
  60.   ?> 
  61.   <form action="" method="post" enctype="multipart/form-data"
  62.    <input type="hidden" name="exphash" value="none"
  63.    <label for="file">Filename:</label> 
  64.    <input type="file" name="file" id="file"
  65.    <input type="submit" name="submit" value="Submit" /><br> 
  66.    <label>Expected hash(optional): <input type="text" name="exphash" size="100"></label> 
  67.    <br><br>Choose an algorithm (This is the list of all the available algorithms in your php installation)<br> 
  68.    <?php 
  69.    foreach(hash_algos() as $algo){ 
  70.     if($algo=='md5'){ 
  71.     echo "<label><input type='radio' name='algo' value='$algo' checked='checked'>$algo</label><br>"
  72.     } else { 
  73.     echo "<label><input type='radio' name='algo' value='$algo'>$algo</label><br>"
  74.     } 
  75.    } 
  76.    ?> 
  77.   </form> 
  78.  <?php 
  79.    } 
  80.   ?> 
  81. </body> 
  82. </html> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临清市| 黄大仙区| 济南市| 金川县| 永年县| 宜君县| 溧水县| 增城市| 册亨县| 公主岭市| 谷城县| 岳阳县| 汶上县| 南乐县| 龙岩市| 轮台县| 册亨县| 白银市| 三门峡市| 肇州县| 大英县| 五台县| 克东县| 潍坊市| 新蔡县| 岑溪市| 平南县| 平邑县| 安达市| 清水河县| 马鞍山市| 肥城市| 融水| 岳池县| 孟连| 凭祥市| 武川县| 汾西县| 延长县| 昭通市| 常德市|