function pleac_Introduction() { $entry = stat('/bin/vi'); $entry = stat('/usr/bin'); $entry = stat($argv[1]); // ------------ $entry = stat('/bin/vi'); $ctime = $entry['ctime']; $size = $entry['size']; // ---------------------------- // For the simple task of determining whether a file contains, text', a simple // function that searches for a newline could be implemented. Not exactly // foolproof, but very simple, low overhead, no installation headaches ... function containsText($file) { $status = FALSE; if ($fp = fopen($file, 'r')) { while (FALSE !== ($char = fgetc($fp))) { if ($char == "\n") { $status = TRUE; break; } } fclose($fp); } return $status; } // PHP offers the [currently experimental] Fileinfo group of functions to // determine file types based on their contents / 'magic numbers'. This // is functionality similar to the *NIX, 'file' utility. Note that it must // first be installed using the PEAR utility [see PHP documentation] function isTextFile($file) { // Note: untested code, but I believe this is how it is supposed to work $finfo = finfo_open(FILEINFO_NONE); $status = finfo_file($finfo, $file) == 'ASCII text'; finfo_close($finfo); return $status; } // Alternatively, use the *NIX utility, 'file', directly function isTextFile($file) { return exec(trim('file -bN ' . escapeshellarg($file))) == 'ASCII text'; } // ---- containsText($argv[1]) || die("File {$argv[1]} doesn't have any text in it\n"); isTextFile($argv[1]) || die("File {$argv[1]} doesn't have any text in it\n"); // ---------------------------- $dirname = '/usr/bin/'; ($dirhdl = opendir($dirname)) || die("Couldn't open {$dirname}\n"); while (($file = readdir($dirhdl)) !== FALSE) { printf("Inside %s is something called: %s\n", $dirname, $file); } closedir($dirhdl); }
function pleac_Processing_All_Files_in_a_Directory() { // Conventional POSIX-like approach to directory traversal $dirname = '/usr/bin/'; ($dirhdl = opendir($dirname)) || die("Couldn't open {$dirname}\n"); while (($file = readdir($dirhdl)) !== FALSE) { // ... do something with $dirname/$file // ... } closedir($dirhdl); // ------------ // Newer [post PHP 4], 'applicative' approach - an array of filenames is // generated that may be processed via external loop ... $dirname = '/usr/bin/'; foreach (scandir($dirname) as $file) { // ... do something with $dirname/$file // ... } // .. or, via callback application, perhaps after massaging by one of the // 'array' family of functions [also uses, 'array_update', from earlier section] $newlist = array_update(array_reverse(scandir($dirname)), create_function('$filelist, $file', ' ; '), array()); // And don't forget that the old standby, 'glob', that returns an array of // paths filtered using the Bourne Shell-based wildcards, '?' and '*', is // also available foreach (glob($dirname . '*') as $path) { // ... do something with $path // ... } // ---------------------------- // Uses, 'isTextFile', from an earlier section $dirname = '/usr/bin/'; echo "Text files in {$dirname}:\n"; foreach (scandir($dirname) as $file) { // Take care when constructing paths to ensure cross-platform operability $path = $dirname . $file; if (is_file($path) && isTextFile($path)) { echo $path . "\n"; } } // ---------------------------- function plain_files($dirname) { ($dirlist = glob($dirname . '*')) || die("Couldn't glob {$dirname}\n"); // Pass function name directly if only a single function performs filter test return array_filter($dirlist, 'is_file'); // Use, 'create_function', if a multi-function test is needed // // return array_filter($dirlist, create_function('$path', 'return is_file($path);')); // } // ------------ foreach (plain_files('/tmp/') as $path) { echo $path . "\n"; } }
function isTextFile($file) { global $ok_extensions; $ext = strtolower(getSuffix($file)); return in_array($ext, $ok_extensions); } $messages = $file_to_edit = $file_content = null; $what = 'edit'; $themes = $_zp_gallery->getThemes(); $theme = basename(sanitize($_GET['theme'])); $themedir = SERVERPATH . '/themes/' . internalToFilesystem($theme); $themefiles = listDirectoryFiles($themedir); $themefiles_to_ext = array(); if (themeIsEditable($theme)) { foreach ($themefiles as $file) { if (isTextFile($file)) { $themefiles_to_ext[getSuffix($file)][] = $file; // array(['php']=>array('file.php', 'image.php'),['css']=>array('style.css')) } else { unset($themefiles[$file]); // $themefile will eventually have all editable files and nothing else } } if (isset($_GET['file'])) { if (!in_array($themedir . '/' . $_GET['file'], $themefiles)) { $messages['errorbox'][] = gettext('Cannot edit this file!'); } $file_to_edit = str_replace('\\', '/', SERVERPATH . '/themes/' . internalToFilesystem($theme) . '/' . sanitize($_GET['file'])); } // Handle POST that updates a file if (isset($_POST['action']) && $_POST['action'] == 'edit_file' && $file_to_edit && !isset($messages['errorbox'])) {
if (isTextFile($file)) { $path_info = pathinfo($file); $themefiles_to_ext[$path_info['extension']][] = $file; // array(['php']=>array('file.php', 'image.php'),['css']=>array('style.css')) } else { unset($themefiles[$file]); // $themefile will eventually have all editable files and nothing else } } // Check that the theme is valid to edit if (!themeIsEditable($theme)) { zp_error(gettext('Cannot edit this theme!')); } // If we're attempting to edit a file that's not a text file or that does not belong to the theme directory, this is an illegal attempt if ($file_to_edit) { if (!in_array($file_to_edit, $themefiles) or !isTextFile($file_to_edit) or filesize($file_to_edit) == 0) { zp_error(gettext('Cannot edit this file!')); } } // realpath() to take care of ../../file.php schemes, str_replace() to sanitize Win32 filenames // Handle POST that updates a file if (isset($_POST['action']) && $_POST['action'] == 'edit_file' && $file_to_edit) { XSRFdefender('edit_theme'); $file_content = sanitize($_POST['newcontent'], 0); $theme = urlencode($theme); if (is_writeable($file_to_edit)) { //is_writable() not always reliable, check return value. see comments @ http://uk.php.net/is_writable $f = @fopen($file_to_edit, 'w+'); if ($f !== FALSE) { @fwrite($f, $file_content); fclose($f);