tempnam_variation3.phpt 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. --TEST--
  2. Test tempnam() function: usage variations - obscure prefixes
  3. --SKIPIF--
  4. <?php
  5. if(substr(PHP_OS, 0, 3) == "WIN")
  6. die("skip Do not run on Windows");
  7. ?>
  8. --CONFLICTS--
  9. obscure_filename
  10. --FILE--
  11. <?php
  12. /* Passing invalid/non-existing args for $prefix */
  13. echo "*** Testing tempnam() with obscure prefixes ***\n";
  14. $file_path = __DIR__."/tempnamVar3";
  15. mkdir($file_path);
  16. /* An array of prefixes */
  17. $names_arr = array(
  18. /* Invalid args */
  19. -1,
  20. TRUE,
  21. FALSE,
  22. "",
  23. " ",
  24. "\0",
  25. array(),
  26. /* prefix with path separator of a non existing directory*/
  27. "/no/such/file/dir",
  28. "php/php"
  29. );
  30. for( $i=0; $i<count($names_arr); $i++ ) {
  31. echo "-- Iteration $i --\n";
  32. try {
  33. $file_name = tempnam("$file_path", $names_arr[$i]);
  34. } catch (Error $e) {
  35. echo $e->getMessage(), "\n";
  36. continue;
  37. }
  38. /* creating the files in existing dir */
  39. if( file_exists($file_name) ) {
  40. echo "File name is => ";
  41. print($file_name);
  42. echo "\n";
  43. echo "File permissions are => ";
  44. printf("%o", fileperms($file_name) );
  45. echo "\n";
  46. echo "File created in => ";
  47. $file_dir = dirname($file_name);
  48. if ($file_dir == sys_get_temp_dir()) {
  49. echo "temp dir\n";
  50. }
  51. else if ($file_dir == $file_path) {
  52. echo "directory specified\n";
  53. }
  54. else {
  55. echo "unknown location\n";
  56. }
  57. }
  58. else {
  59. echo "-- File is not created --\n";
  60. }
  61. unlink($file_name);
  62. }
  63. rmdir($file_path);
  64. ?>
  65. --EXPECTF--
  66. *** Testing tempnam() with obscure prefixes ***
  67. -- Iteration 0 --
  68. File name is => %s/%s
  69. File permissions are => 100600
  70. File created in => directory specified
  71. -- Iteration 1 --
  72. File name is => %s/%s
  73. File permissions are => 100600
  74. File created in => directory specified
  75. -- Iteration 2 --
  76. File name is => %s/%s
  77. File permissions are => 100600
  78. File created in => directory specified
  79. -- Iteration 3 --
  80. File name is => %s/%s
  81. File permissions are => 100600
  82. File created in => directory specified
  83. -- Iteration 4 --
  84. File name is => %s/%s
  85. File permissions are => 100600
  86. File created in => directory specified
  87. -- Iteration 5 --
  88. tempnam(): Argument #2 ($prefix) must not contain any null bytes
  89. -- Iteration 6 --
  90. tempnam(): Argument #2 ($prefix) must be of type string, array given
  91. -- Iteration 7 --
  92. File name is => %s/dir%s
  93. File permissions are => 100600
  94. File created in => directory specified
  95. -- Iteration 8 --
  96. File name is => %s/php%s
  97. File permissions are => 100600
  98. File created in => directory specified