md5_file.phpt 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. --TEST--
  2. Test md5_file() function with ASCII output and raw binary output
  3. --FILE--
  4. <?php
  5. /* Creating an empty file */
  6. if (($handle = fopen( "EmptyFileMD5.txt", "w+")) == FALSE)
  7. return false;
  8. /* Creating a data file */
  9. if (($handle2 = fopen( "DataFileMD5.txt", "w+")) == FALSE)
  10. return false;
  11. /* Writing into file */
  12. $filename = "DataFileMD5.txt";
  13. $content = "Add this to the file\n";
  14. if (is_writable($filename)) {
  15. if (fwrite($handle2, $content) === FALSE) {
  16. echo "Cannot write to file ($filename)";
  17. exit;
  18. }
  19. }
  20. // close the files
  21. fclose($handle);
  22. fclose($handle2);
  23. /* Testing error conditions */
  24. echo "\n*** Testing for error conditions ***\n";
  25. /* No filename */
  26. try {
  27. var_dump( md5_file("") );
  28. } catch (\ValueError $e) {
  29. echo $e->getMessage() . \PHP_EOL;
  30. }
  31. /* invalid filename */
  32. var_dump( md5_file("aZrq16u") );
  33. /* Scalar value as filename */
  34. var_dump( md5_file(12) );
  35. /* Hexadecimal Output for Empty file as input */
  36. echo "\n*** Hexadecimal Output for Empty file as Argument ***\n";
  37. var_dump( md5_file("EmptyFileMD5.txt") );
  38. /* Raw Binary Output for Empty file as input */
  39. echo "\n*** Raw Binary Output for Empty file as Argument ***\n";
  40. var_dump( md5_file("EmptyFileMD5.txt", true) );
  41. /* Normal operation with hexadecimal output */
  42. echo "\n*** Hexadecimal Output for a valid file with some contents ***\n";
  43. var_dump( md5_file("DataFileMD5.txt") );
  44. /* Normal operation with raw binary output */
  45. echo "\n*** Raw Binary Output for a valid file with some contents ***\n";
  46. var_dump ( md5_file("DataFileMD5.txt", true) );
  47. // remove temp files
  48. unlink("DataFileMD5.txt");
  49. unlink("EmptyFileMD5.txt");
  50. echo "\nDone";
  51. ?>
  52. --EXPECTF--
  53. *** Testing for error conditions ***
  54. Path cannot be empty
  55. Warning: md5_file(aZrq16u): Failed to open stream: No such file or directory in %s on line %d
  56. bool(false)
  57. Warning: md5_file(12): Failed to open stream: No such file or directory in %s on line %d
  58. bool(false)
  59. *** Hexadecimal Output for Empty file as Argument ***
  60. string(32) "d41d8cd98f00b204e9800998ecf8427e"
  61. *** Raw Binary Output for Empty file as Argument ***
  62. string(16) "ÔŒÙ�%0²é€ ˜ìøB~"
  63. *** Hexadecimal Output for a valid file with some contents ***
  64. string(32) "7f28ec647825e2a70bf67778472cd4a2"
  65. *** Raw Binary Output for a valid file with some contents ***
  66. string(16) "(ìdx%⧠öwxG,Ô¢"
  67. Done