Php method for file hash computation _ PHP Tutorial

Source: Internet
Author: User
Php performs the hash operation on files. Php performs the following hash operation on files: this code is very useful. if you download an object, the website provides the hash result, you can perform the php hash operation on the downloaded file.

The details are as follows:

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

Hash (Check) Files

If (! Empty ($ _ FILES )){

If ($ _ FILES ["file"] ["error"]> 0 ){

Switch ($ _ FILES ["file"] ["error"]) {

Case 1:

Echo"Error: The uploaded file exceeds the upload_max_filesize directive in php. ini
";

Break;

Case 2:

Echo"Error: The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
";

Break;

Case 3:

Echo"Error: The uploaded file was only partially uploaded.
";

Break;

Case 4:

Echo"Error: No file was uploaded.
";

Break;

Case 6:

Echo"Error: Missing a temporary folder.
";

Break;

Case 7:

Echo"Error: Failed to write file to disk.
";

Break;

Case 8:

Echo"Error: a php extension stopped the file upload.
";

Break;

Default:

Echo"Unknown error occured.
";

}

} Else {

Echo 'upload: '. $ _ FILES ['file'] ['name'].'
';

Echo 'type: '. $ _ FILES ['file'] ['type'].'
';

Echo 'Size: '. (round ($ _ FILES ['file'] ['size']/1024, 2). 'kb

';

If (array_search ($ _ POST ['algo'], hash_algos () = false ){

Echo 'unknown hashing algorithm requested.
';

} Else {

Echo 'hashing Algorithm: '. $ _ POST ['algo'].'
';

$ Hash = hash_file ($ _ POST ['algo'], $ _ FILES ['file'] ['tmp _ name']);

Echo 'calculated hash: '. $ hash .'
';

If ($ _ POST ['expash ']! = 'None '&&! Empty ($ _ POST ['expyash ']) {

Echo 'expected hash: '. $ _ POST ['expyash'].'

';

Echo ($ hash = $ _ POST ['expyash '])? 'Hash matched expected value.':'Hash did not match expected value.';

Echo'
';

}

}

}

?>


Again

} Else {

?>

}

?>

Example: This code is very useful. if you download an object and the website provides the hash result, you can download the downloaded file...

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.