首页 > 后端开发 > php教程 > php对文件进行hash运算

php对文件进行hash运算

WBOY
发布: 2016-07-25 08:43:18
原创
1055 人浏览过

这段代码非常有用,如果你下载了一个文件,网站提供了hash结果,你可以对你下载下来的文件进行hash运算,以验证下载的文件是否正确。

  1. Hash (Check) Files
  2. if(!empty($_FILES)){
  3. if ($_FILES["file"]["error"] > 0){
  4. switch($_FILES["file"]["error"]){
  5. case 1:
  6. echo "Error: The uploaded file exceeds the upload_max_filesize directive in php.ini
    ";
  7. break;
  8. case 2:
  9. echo "Error: The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
    ";
  10. break;
  11. case 3:
  12. echo "Error: The uploaded file was only partially uploaded.
    ";
  13. break;
  14. case 4:
  15. echo "Error: No file was uploaded.
    ";
  16. break;
  17. case 6:
  18. echo "Error: Missing a temporary folder.
    ";
  19. break;
  20. case 7:
  21. echo "Error: Failed to write file to disk.
    ";
  22. break;
  23. case 8:
  24. echo "Error: A PHP extension stopped the file upload.
    ";
  25. break;
  26. default:
  27. echo "Unknown error occured.
    ";
  28. }
  29. } else {
  30. echo 'Upload: ' . $_FILES['file']['name'] . '
    ';
  31. echo 'Type: ' . $_FILES['file']['type'] . '
    ';
  32. echo 'Size: ' . (round($_FILES['file']['size'] / 1024, 2)) . ' Kb

    ';
  33. if(array_search($_POST['algo'], hash_algos())===false){
  34. echo 'Unknown hashing algorithm requested.
    ';
  35. } else {
  36. echo 'Hashing Algorithm: '. $_POST['algo'] . '
    ';
  37. $hash = hash_file($_POST['algo'], $_FILES['file']['tmp_name']);
  38. echo 'Calculated hash: ' . $hash . '
    ';
  39. if($_POST['exphash']!=='none' && !empty($_POST['exphash'])){
  40. echo 'Expected hash:   ' . $_POST['exphash'] . '

    ';
  41. echo ($hash==$_POST['exphash'])? 'Hash matched expected value.' : 'Hash did not match expected value.';
  42. echo '
    ';
  43. }
  44. }
  45. }
  46. ?>

  47. } else {
  48. ?>



  49. Choose an algorithm (This is the list of all the available algorithms in your php installation)
  50. foreach(hash_algos() as $algo){
  51. if($algo=='md5'){
  52. echo "
    ";
  53. } else {
  54. echo "
    ";
  55. }
  56. }
  57. ?>
  58. }
  59. ?>
复制代码

php, hash


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板