首页 > 开发 > PHP > 正文

php对文件进行hash运算的方法

2024-05-04 23:33:53
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了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(!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. } 
  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' && !empty($_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> 

希望本文所述对大家的php程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表