Php performs file hash operations

Source: Internet
Author: User
Php performs file hash operations

This code is very useful. if you download a file and the website provides the hash result, you can perform a hash operation on the downloaded file to verify that the downloaded file is correct.

  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 ['expash ']! = 'None '&&! Empty ($ _ POST ['expyash ']) {
  40. Echo 'expected hash: '. $ _ POST ['expyash'].'

    ';
  41. Echo ($ hash = $ _ POST ['expyash '])? 'Hash matched expected value.':'Hash did not match expected value.';
  42. Echo'
    ';
  43. }
  44. }
  45. }
  46. ?>

  47. Again
  48. } Else {
  49. ?>
  50. }
  51. ?>


Php, hash

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.