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