1--TEST-- 2Test tempnam() function: usage variations - obscure prefixes 3--SKIPIF-- 4<?php 5if(substr(PHP_OS, 0, 3) == "WIN") 6 die("skip Do not run on Windows"); 7?> 8--FILE-- 9<?php 10/* Prototype: string tempnam ( string $dir, string $prefix ); 11 Description: Create file with unique file name. 12*/ 13 14/* Passing invalid/non-existing args for $prefix */ 15 16echo "*** Testing tempnam() with obscure prefixes ***\n"; 17$file_path = dirname(__FILE__)."/tempnamVar3"; 18mkdir($file_path); 19 20/* An array of prefixes */ 21$names_arr = array( 22 /* Invalid args */ 23 -1, 24 TRUE, 25 FALSE, 26 NULL, 27 "", 28 " ", 29 "\0", 30 array(), 31 32 /* prefix with path separator of a non existing directory*/ 33 "/no/such/file/dir", 34 "php/php" 35 36); 37 38for( $i=0; $i<count($names_arr); $i++ ) { 39 echo "-- Iteration $i --\n"; 40 $file_name = tempnam("$file_path", $names_arr[$i]); 41 42 /* creating the files in existing dir */ 43 if( file_exists($file_name) ) { 44 echo "File name is => "; 45 print($file_name); 46 echo "\n"; 47 48 echo "File permissions are => "; 49 printf("%o", fileperms($file_name) ); 50 echo "\n"; 51 52 echo "File created in => "; 53 $file_dir = dirname($file_name); 54 55 if ($file_dir == sys_get_temp_dir()) { 56 echo "temp dir\n"; 57 } 58 else if ($file_dir == $file_path) { 59 echo "directory specified\n"; 60 } 61 else { 62 echo "unknown location\n"; 63 } 64 65 } 66 else { 67 echo "-- File is not created --\n"; 68 } 69 70 unlink($file_name); 71} 72 73rmdir($file_path); 74echo "\n*** Done ***\n"; 75?> 76--EXPECTF-- 77*** Testing tempnam() with obscure prefixes *** 78-- Iteration 0 -- 79File name is => %s/%s 80File permissions are => 100600 81File created in => directory specified 82-- Iteration 1 -- 83File name is => %s/%s 84File permissions are => 100600 85File created in => directory specified 86-- Iteration 2 -- 87File name is => %s/%s 88File permissions are => 100600 89File created in => directory specified 90-- Iteration 3 -- 91File name is => %s/%s 92File permissions are => 100600 93File created in => directory specified 94-- Iteration 4 -- 95File name is => %s/%s 96File permissions are => 100600 97File created in => directory specified 98-- Iteration 5 -- 99File name is => %s/%s 100File permissions are => 100600 101File created in => directory specified 102-- Iteration 6 -- 103 104Warning: tempnam() expects parameter 2 to be a valid path, string given in %s on line %d 105-- File is not created -- 106 107Warning: unlink(): %s in %s on line %d 108-- Iteration 7 -- 109 110Warning: tempnam() expects parameter 2 to be a valid path, array given in %s on line %d 111-- File is not created -- 112 113Warning: unlink(): %s in %s on line %d 114-- Iteration 8 -- 115File name is => %s/dir%s 116File permissions are => 100600 117File created in => directory specified 118-- Iteration 9 -- 119File name is => %s/php%s 120File permissions are => 100600 121File created in => directory specified 122 123*** Done *** 124 125