note 111313 added to function.posix-getpwuid

From: Date: Tue, 05 Feb 2013 15:37:24 +0000
Subject: note 111313 added to function.posix-getpwuid
Groups: php.notes 
Request: Send a blank email to php-notes+get-193533@lists.php.net to get a copy of this message
I only want the user name rather than the rest. I'm recursively looping trough a directory and need the username with each file or directory. I use stat to get file attributes that I need which gives me uid. Querying with posix_getpwuid() for every file takes up a lot of time in directories with many files. I came up with a caching mechanism (which I believe should be built-in). Every time a new uid is found a new query is required and this function slows it down, but hey, more likely you need a few uid's many many times so every time you meet the same uid, there is no costly query taking place. Heres my code, feel free, etc., etc. <?php $arr_uname = Array(); function file_owner_cached($uid) { global $arr_uname; if (!isset($arr_uname[$uid])) { $arr_uname[$uid] = posix_getpwuid($uid)['name']; } return $arr_uname[$uid]; } ?> Works in PHP 5.3.19, under linux of course.. not tested on anything else. ---- Server IP: 69.147.83.199 Probable Submitter: 95.97.249.34 ---- Manual Page -- http://php.net/manual/en/function.posix-getpwuid.php Edit -- https://master.php.net/note/edit/111313 Del: integrated -- https://master.php.net/note/delete/111313/integrated Del: useless -- https://master.php.net/note/delete/111313/useless Del: bad code -- https://master.php.net/note/delete/111313/bad+code Del: spam -- https://master.php.net/note/delete/111313/spam Del: non-english -- https://master.php.net/note/delete/111313/non-english Del: in docs -- https://master.php.net/note/delete/111313/in+docs Del: other reasons-- https://master.php.net/note/delete/111313 Reject -- https://master.php.net/note/reject/111313 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#193533) next »