blob: cf00f6b562c8dfa7fa053dfe796de24b6deb5f29 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02008 *
Instructor, BCIT0e59db62019-01-01 08:34:36 -08009 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
Andrey Andreev8bf6bb62012-01-06 16:11:04 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Instructor, BCIT0e59db62019-01-01 08:34:36 -080032 * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33 * @license https://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * CodeIgniter File Helpers
42 *
43 * @package CodeIgniter
44 * @subpackage Helpers
45 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/helpers/file_helper.html
Derek Allard2067d1a2008-11-13 22:59:24 +000048 */
49
50// ------------------------------------------------------------------------
51
Derek Allard2067d1a2008-11-13 22:59:24 +000052if ( ! function_exists('read_file'))
53{
Timothy Warren01b129a2012-04-27 11:36:50 -040054 /**
55 * Read File
56 *
Calvin Tam55bc5052015-07-24 02:27:24 -070057 * Opens the file specified in the path and returns it as a string.
Timothy Warren01b129a2012-04-27 11:36:50 -040058 *
Andrey Andreev29d909d2012-10-27 01:05:09 +030059 * @todo Remove in version 3.1+.
60 * @deprecated 3.0.0 It is now just an alias for PHP's native file_get_contents().
61 * @param string $file Path to file
62 * @return string File contents
Timothy Warren01b129a2012-04-27 11:36:50 -040063 */
Derek Allard2067d1a2008-11-13 22:59:24 +000064 function read_file($file)
65 {
Andrey Andreev596c51c2012-06-07 15:51:58 +030066 return @file_get_contents($file);
Derek Allard2067d1a2008-11-13 22:59:24 +000067 }
68}
Barry Mienydd671972010-10-04 16:33:58 +020069
Derek Allard2067d1a2008-11-13 22:59:24 +000070// ------------------------------------------------------------------------
71
Derek Allard2067d1a2008-11-13 22:59:24 +000072if ( ! function_exists('write_file'))
73{
Timothy Warren01b129a2012-04-27 11:36:50 -040074 /**
75 * Write File
76 *
77 * Writes data to the file specified in the path.
78 * Creates a new file if non-existent.
79 *
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020080 * @param string $path File path
81 * @param string $data Data to write
82 * @param string $mode fopen() mode (default: 'wb')
Timothy Warren01b129a2012-04-27 11:36:50 -040083 * @return bool
84 */
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +020085 function write_file($path, $data, $mode = 'wb')
Derek Allard2067d1a2008-11-13 22:59:24 +000086 {
87 if ( ! $fp = @fopen($path, $mode))
88 {
89 return FALSE;
90 }
Barry Mienydd671972010-10-04 16:33:58 +020091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 flock($fp, LOCK_EX);
Andrey Andreevd8b1ad32014-01-15 17:42:52 +020093
Andrey Andreev1f5090a2014-06-03 15:40:30 +030094 for ($result = $written = 0, $length = strlen($data); $written < $length; $written += $result)
Andrey Andreevd8b1ad32014-01-15 17:42:52 +020095 {
96 if (($result = fwrite($fp, substr($data, $written))) === FALSE)
97 {
98 break;
99 }
100 }
101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 flock($fp, LOCK_UN);
Barry Mienydd671972010-10-04 16:33:58 +0200103 fclose($fp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000104
Andrey Andreevd8b1ad32014-01-15 17:42:52 +0200105 return is_int($result);
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 }
107}
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109// ------------------------------------------------------------------------
110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111if ( ! function_exists('delete_files'))
112{
Timothy Warren01b129a2012-04-27 11:36:50 -0400113 /**
114 * Delete Files
115 *
116 * Deletes all files contained in the supplied directory path.
117 * Files must be writable or owned by the system in order to be deleted.
118 * If the second parameter is set to TRUE, any directories contained
119 * within the supplied base directory will be nuked as well.
120 *
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200121 * @param string $path File path
122 * @param bool $del_dir Whether to delete any directories found in the path
123 * @param bool $htdocs Whether to skip deleting .htaccess and index page files
124 * @param int $_level Current directory depth level (default: 0; internal use only)
Timothy Warren01b129a2012-04-27 11:36:50 -0400125 * @return bool
126 */
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200127 function delete_files($path, $del_dir = FALSE, $htdocs = FALSE, $_level = 0)
Barry Mienydd671972010-10-04 16:33:58 +0200128 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 // Trim the trailing slash
Andrey Andreev256a18c2012-10-23 12:18:32 +0300130 $path = rtrim($path, '/\\');
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 if ( ! $current_dir = @opendir($path))
Greg Aker3e9519e2010-01-15 00:09:34 +0000133 {
Barry Mienydd671972010-10-04 16:33:58 +0200134 return FALSE;
Greg Aker3e9519e2010-01-15 00:09:34 +0000135 }
Barry Mienydd671972010-10-04 16:33:58 +0200136
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500137 while (FALSE !== ($filename = @readdir($current_dir)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
Andrey Andreevb2518642012-03-26 21:44:08 +0300139 if ($filename !== '.' && $filename !== '..')
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Andrey Andreevf7b028b2016-06-22 12:42:44 +0300141 $filepath = $path.DIRECTORY_SEPARATOR.$filename;
142
143 if (is_dir($filepath) && $filename[0] !== '.' && ! is_link($filepath))
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
Andrey Andreevf7b028b2016-06-22 12:42:44 +0300145 delete_files($filepath, $del_dir, $htdocs, $_level + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 }
Andrey Andreev27228c92012-07-27 10:36:29 +0300147 elseif ($htdocs !== TRUE OR ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename))
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 {
Andrey Andreevf7b028b2016-06-22 12:42:44 +0300149 @unlink($filepath);
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
151 }
152 }
Barry Mienydd671972010-10-04 16:33:58 +0200153
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200154 closedir($current_dir);
Barry Mienydd671972010-10-04 16:33:58 +0200155
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200156 return ($del_dir === TRUE && $_level > 0)
157 ? @rmdir($path)
158 : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 }
160}
161
162// ------------------------------------------------------------------------
163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164if ( ! function_exists('get_filenames'))
165{
Timothy Warren01b129a2012-04-27 11:36:50 -0400166 /**
167 * Get Filenames
168 *
169 * Reads the specified directory and builds an array containing the filenames.
170 * Any sub-folders contained within the specified path are read as well.
171 *
172 * @param string path to source
173 * @param bool whether to include the path as part of the filename
174 * @param bool internal variable to determine recursion status - do not use in calls
175 * @return array
176 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
178 {
179 static $_filedata = array();
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 if ($fp = @opendir($source_dir))
182 {
183 // reset the array and make sure $source_dir has a trailing slash on the initial call
184 if ($_recursion === FALSE)
185 {
186 $_filedata = array();
187 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
188 }
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 while (FALSE !== ($file = readdir($fp)))
191 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200192 if (is_dir($source_dir.$file) && $file[0] !== '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 {
Barry Mienydd671972010-10-04 16:33:58 +0200194 get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 }
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200196 elseif ($file[0] !== '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100198 $_filedata[] = ($include_path === TRUE) ? $source_dir.$file : $file;
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
200 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200201
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200202 closedir($fp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 return $_filedata;
204 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200205
206 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 }
208}
209
210// --------------------------------------------------------------------
211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212if ( ! function_exists('get_dir_file_info'))
213{
Timothy Warren01b129a2012-04-27 11:36:50 -0400214 /**
215 * Get Directory File Information
216 *
217 * Reads the specified directory and builds an array containing the filenames,
218 * filesize, dates, and permissions
219 *
220 * Any sub-folders contained within the specified path are read as well.
221 *
222 * @param string path to source
223 * @param bool Look only at the top level directory specified?
224 * @param bool internal variable to determine recursion status - do not use in calls
225 * @return array
226 */
Derek Jones788b00f2009-11-27 18:00:20 +0000227 function get_dir_file_info($source_dir, $top_level_only = TRUE, $_recursion = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
Derek Jones63dc27f2009-02-10 21:59:20 +0000229 static $_filedata = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 $relative_path = $source_dir;
Derek Jonesc37f0e12009-04-21 13:33:40 +0000231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 if ($fp = @opendir($source_dir))
233 {
234 // reset the array and make sure $source_dir has a trailing slash on the initial call
235 if ($_recursion === FALSE)
236 {
237 $_filedata = array();
238 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
239 }
240
Andrey Andreevf9a615a2014-09-28 20:24:06 +0300241 // Used to be foreach (scandir($source_dir, 1) as $file), but scandir() is simply not as fast
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 while (FALSE !== ($file = readdir($fp)))
243 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200244 if (is_dir($source_dir.$file) && $file[0] !== '.' && $top_level_only === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
Barry Mienydd671972010-10-04 16:33:58 +0200246 get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $top_level_only, TRUE);
247 }
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200248 elseif ($file[0] !== '.')
Barry Mienydd671972010-10-04 16:33:58 +0200249 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 $_filedata[$file] = get_file_info($source_dir.$file);
251 $_filedata[$file]['relative_path'] = $relative_path;
252 }
253 }
Derek Jones788b00f2009-11-27 18:00:20 +0000254
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200255 closedir($fp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 return $_filedata;
257 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200258
259 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
261}
262
263// --------------------------------------------------------------------
264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265if ( ! function_exists('get_file_info'))
266{
Timothy Warren01b129a2012-04-27 11:36:50 -0400267 /**
268 * Get File Info
269 *
270 * Given a file and path, returns the name, path, size, date modified
271 * Second parameter allows you to explicitly declare what information you want returned
272 * Options are: name, server_path, size, date, readable, writable, executable, fileperms
273 * Returns FALSE if the file cannot be found.
274 *
275 * @param string path to file
276 * @param mixed array or comma separated string of information returned
277 * @return array
278 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date'))
280 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 if ( ! file_exists($file))
282 {
283 return FALSE;
284 }
285
286 if (is_string($returned_values))
287 {
288 $returned_values = explode(',', $returned_values);
289 }
290
291 foreach ($returned_values as $key)
292 {
293 switch ($key)
294 {
295 case 'name':
Andrey Andreev12526cd2013-07-19 13:06:51 +0300296 $fileinfo['name'] = basename($file);
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 break;
298 case 'server_path':
299 $fileinfo['server_path'] = $file;
300 break;
301 case 'size':
302 $fileinfo['size'] = filesize($file);
303 break;
304 case 'date':
Robin Sowell73c19fc2010-04-09 11:18:26 -0400305 $fileinfo['date'] = filemtime($file);
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 break;
307 case 'readable':
308 $fileinfo['readable'] = is_readable($file);
309 break;
310 case 'writable':
Andrey Andreev03077f92014-02-07 14:53:39 +0200311 $fileinfo['writable'] = is_really_writable($file);
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 break;
313 case 'executable':
314 $fileinfo['executable'] = is_executable($file);
315 break;
316 case 'fileperms':
317 $fileinfo['fileperms'] = fileperms($file);
318 break;
319 }
320 }
321
322 return $fileinfo;
323 }
324}
325
326// --------------------------------------------------------------------
327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328if ( ! function_exists('get_mime_by_extension'))
329{
Timothy Warren01b129a2012-04-27 11:36:50 -0400330 /**
331 * Get Mime by Extension
332 *
333 * Translates a file extension into a mime type based on config/mimes.php.
334 * Returns FALSE if it can't determine the type, or open the mime config file
335 *
336 * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience
337 * It should NOT be trusted, and should certainly NOT be used for security
338 *
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200339 * @param string $filename File name
340 * @return string
Timothy Warren01b129a2012-04-27 11:36:50 -0400341 */
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200342 function get_mime_by_extension($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 {
Phil Sturgeon39b1c112012-06-04 16:51:14 -0500344 static $mimes;
Derek Jonesc37f0e12009-04-21 13:33:40 +0000345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 if ( ! is_array($mimes))
347 {
Andrey Andreev48844d12015-10-05 10:52:04 +0300348 $mimes = get_mimes();
Eric Barnes92808342011-03-18 09:02:37 -0400349
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300350 if (empty($mimes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 {
352 return FALSE;
353 }
354 }
355
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200356 $extension = strtolower(substr(strrchr($filename, '.'), 1));
357
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300358 if (isset($mimes[$extension]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300360 return is_array($mimes[$extension])
361 ? current($mimes[$extension]) // Multiple mime types, just give the first one
362 : $mimes[$extension];
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200364
365 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 }
367}
368
369// --------------------------------------------------------------------
370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371if ( ! function_exists('symbolic_permissions'))
372{
Timothy Warren01b129a2012-04-27 11:36:50 -0400373 /**
374 * Symbolic Permissions
375 *
376 * Takes a numeric value representing a file's permissions and returns
377 * standard symbolic notation representing that value
378 *
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200379 * @param int $perms Permissions
Timothy Warren01b129a2012-04-27 11:36:50 -0400380 * @return string
381 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 function symbolic_permissions($perms)
Barry Mienydd671972010-10-04 16:33:58 +0200383 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200384 if (($perms & 0xC000) === 0xC000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
386 $symbolic = 's'; // Socket
387 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200388 elseif (($perms & 0xA000) === 0xA000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
390 $symbolic = 'l'; // Symbolic Link
391 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200392 elseif (($perms & 0x8000) === 0x8000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 {
394 $symbolic = '-'; // Regular
395 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200396 elseif (($perms & 0x6000) === 0x6000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
398 $symbolic = 'b'; // Block special
399 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200400 elseif (($perms & 0x4000) === 0x4000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
402 $symbolic = 'd'; // Directory
403 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200404 elseif (($perms & 0x2000) === 0x2000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
406 $symbolic = 'c'; // Character special
407 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200408 elseif (($perms & 0x1000) === 0x1000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
410 $symbolic = 'p'; // FIFO pipe
411 }
412 else
413 {
414 $symbolic = 'u'; // Unknown
415 }
416
417 // Owner
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200418 $symbolic .= (($perms & 0x0100) ? 'r' : '-')
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200419 .(($perms & 0x0080) ? 'w' : '-')
420 .(($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000421
422 // Group
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200423 $symbolic .= (($perms & 0x0020) ? 'r' : '-')
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200424 .(($perms & 0x0010) ? 'w' : '-')
425 .(($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000426
427 // World
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200428 $symbolic .= (($perms & 0x0004) ? 'r' : '-')
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200429 .(($perms & 0x0002) ? 'w' : '-')
430 .(($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000431
Barry Mienydd671972010-10-04 16:33:58 +0200432 return $symbolic;
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 }
434}
435
436// --------------------------------------------------------------------
437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438if ( ! function_exists('octal_permissions'))
439{
Timothy Warren01b129a2012-04-27 11:36:50 -0400440 /**
441 * Octal Permissions
442 *
443 * Takes a numeric value representing a file's permissions and returns
444 * a three character string representing the file's octal permissions
445 *
Andrey Andreevf6d9a7c2012-11-08 17:27:57 +0200446 * @param int $perms Permissions
Timothy Warren01b129a2012-04-27 11:36:50 -0400447 * @return string
448 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 function octal_permissions($perms)
450 {
451 return substr(sprintf('%o', $perms), -3);
452 }
453}