当前位置:Gxlcms > PHP教程 > php对文件进行hash运算

php对文件进行hash运算

时间:2021-07-01 10:21:17 帮助过:4人阅读

这段代码非常有用,如果你下载了一个文件,网站提供了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. }
  50. ?>

php, hash

人气教程排行