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

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

php對(duì)文件進(jìn)行hash運(yùn)算的方法

2024-05-04 23:33:53
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

這篇文章主要介紹了php對(duì)文件進(jìn)行hash運(yùn)算的方法,涉及針對(duì)文件的hash運(yùn)算技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了php對(duì)文件進(jìn)行hash運(yùn)算的方法。分享給大家供大家參考。具體如下:

這段代碼非常有用,如果你下載了一個(gè)文件,網(wǎng)站提供了hash結(jié)果,你可以對(duì)你下載下來(lái)的文件進(jìn)行hash運(yùn)算,以驗(yàn)證下載的文件是否正確。

 

 
  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(!empty($_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. } else { 
  38. echo 'Upload: ' . $_FILES['file']['name'] . '<br>'; 
  39. echo 'Type: ' . $_FILES['file']['type'] . '<br>'; 
  40. echo 'Size: ' . (round($_FILES['file']['size'] / 1024, 2)) . ' Kb<br><br>'; 
  41. if(array_search($_POST['algo'], hash_algos())===false){ 
  42. echo 'Unknown hashing algorithm requested.<br>'; 
  43. } else { 
  44. echo 'Hashing Algorithm: '. $_POST['algo'] . '<br>'; 
  45. $hash = hash_file($_POST['algo'], $_FILES['file']['tmp_name']); 
  46. echo 'Calculated hash: ' . $hash . '<br>'; 
  47. if($_POST['exphash']!=='none' && !empty($_POST['exphash'])){ 
  48. echo 'Expected hash: ' . $_POST['exphash'] . '<br><br>'; 
  49. echo ($hash==$_POST['exphash'])? '<b id="ok">Hash matched expected value.</b>' : '<b id="nono">Hash did not match expected value.</b>'; 
  50. echo '<br>'; 
  51. ?> 
  52. <br> 
  53. <button onClick="document.location.reload(true)">Again</button> 
  54. <?php 
  55. } else { 
  56. ?> 
  57. <form action="" method="post" enctype="multipart/form-data"> 
  58. <input type="hidden" name="exphash" value="none"> 
  59. <label for="file">Filename:</label> 
  60. <input type="file" name="file" id="file"> 
  61. <input type="submit" name="submit" value="Submit" /><br> 
  62. <label>Expected hash(optional): <input type="text" name="exphash" size="100"></label> 
  63. <br><br>Choose an algorithm (This is the list of all the available algorithms in your php installation)<br> 
  64. <?php 
  65. foreach(hash_algos() as $algo){ 
  66. if($algo=='md5'){ 
  67. echo "<label><input type='radio' name='algo' value='$algo' checked='checked'>$algo</label><br>"; 
  68. } else { 
  69. echo "<label><input type='radio' name='algo' value='$algo'>$algo</label><br>"; 
  70. ?> 
  71. </form> 
  72. <?php 
  73. ?> 
  74. </body> 
  75. </html> 

希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 肃宁县| 长白| 灌阳县| 宁波市| 洛宁县| 黑水县| 松潘县| 华亭县| 闽清县| 灌云县| 无锡市| 贵定县| 龙游县| 新巴尔虎左旗| 彩票| 吉木乃县| 惠东县| 左贡县| 三河市| 嘉义市| 包头市| 佳木斯市| 临江市| 阿拉善左旗| 合作市| 永靖县| 荆门市| 防城港市| 沐川县| 淳化县| 定安县| 尖扎县| 徐闻县| 额尔古纳市| 吉安县| 海晏县| 青神县| 龙南县| 大丰市| 涞水县| 东海县|