note 96027 added to function.tempnam

From: Date: Wed, 03 Feb 2010 21:16:07 +0000
Subject: note 96027 added to function.tempnam
Groups: php.notes 
Request: Send a blank email to php-notes+get-165640@lists.php.net to get a copy of this message
I needed a function that allowed me to specify a filename suffix and a longer prefix: <?php function my_tempnam($prefix = null, $suffix = null, $dir = null) { func_num_args() > 3 and exit(__FUNCTION__.'(): passed '.func_num_args().' args, should pass 0, 1, 2, or 3 args. Usage: '.__FUNCTION__.'(optional filename prefix, optional filename suffix, optional directory)'); $prefix = trim($prefix); $suffix = trim($suffix); $dir = trim($dir); empty($dir) and $dir = trim(sys_get_temp_dir()); empty($dir) and exit(__FUNCTION__.'(): could not get system temp dir'); is_dir($dir) or exit(__FUNCTION__."(): \"$dir\" is not a directory"); // posix valid filename characters. exclude "similar" characters 0, O, 1, l, I to enhance readability. add - _ $fn_chars = array_flip(array_diff(array_merge(range(50,57), range(65,90), range(97,122), array(95,45)), array(73,79,108))); // create random filename 20 chars long for security for($fn = rtrim($dir, '/') . '/' . $prefix, $loop = 0, $x = 0; $x++ < 20; $fn .= chr(array_rand($fn_chars))); while (file_exists($fn.$suffix)) { $fn .= chr(array_rand($fn_chars)); $loop++ > 10 and exit(__FUNCTION__."(): looped too many times trying to create a unique file name in directory \"$dir\""); clearstatcache(); } $fn = $fn.$suffix; touch($fn) or exit(__FUNCTION__."(): could not create tmp file \"$fn\""); return $fn; } ?> ---- Server IP: 208.69.120.55 Probable Submitter: 65.41.147.195 ---- Manual Page -- http://www.php.net/manual/en/function.tempnam.php Edit -- https://master.php.net/note/edit/96027 Del: integrated -- https://master.php.net/note/delete/96027/integrated Del: useless -- https://master.php.net/note/delete/96027/useless Del: bad code -- https://master.php.net/note/delete/96027/bad+code Del: spam -- https://master.php.net/note/delete/96027/spam Del: non-english -- https://master.php.net/note/delete/96027/non-english Del: in docs -- https://master.php.net/note/delete/96027/in+docs Del: other reasons-- https://master.php.net/note/delete/96027 Reject -- https://master.php.net/note/reject/96027 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#165640) next »