📄 system.php
字号:
<?php//// +----------------------------------------------------------------------+// | PHP Version 5 |// +----------------------------------------------------------------------+// | Copyright (c) 1997-2004 The PHP Group |// +----------------------------------------------------------------------+// | This source file is subject to version 2.0 of the PHP license, |// | that is bundled with this package in the file LICENSE, and is |// | available through the world-wide-web at the following url: |// | http://www.php.net/license/3_0.txt. |// | If you did not receive a copy of the PHP license and are unable to |// | obtain it through the world-wide-web, please send a note to |// | license@php.net so we can mail you a copy immediately. |// +----------------------------------------------------------------------+// | Authors: Tomas V.V.Cox <cox@idecnet.com> |// +----------------------------------------------------------------------+//// $Id: System.php,v 1.35 2004/01/19 03:32:12 cellog Exp $//require_once 'PEAR.php';require_once 'Console/Getopt.php';$GLOBALS['_System_temp_files'] = array();/*** System offers cross plattform compatible system functions** Static functions for different operations. Should work under* Unix and Windows. The names and usage has been taken from its respectively* GNU commands. The functions will return (bool) false on error and will* trigger the error with the PHP trigger_error() function (you can silence* the error by prefixing a '@' sign after the function call).** Documentation on this class you can find in:* http://pear.php.net/manual/** Example usage:* if (!@System::rm('-r file1 dir1')) {* print "could not delete file1 or dir1";* }** In case you need to to pass file names with spaces,* pass the params as an array:** System::rm(array('-r', $file1, $dir1));** @package System* @author Tomas V.V.Cox <cox@idecnet.com>* @version $Revision: 1.35 $* @access public* @see http://pear.php.net/manual/*/class System{ /** * returns the commandline arguments of a function * * @param string $argv the commandline * @param string $short_options the allowed option short-tags * @param string $long_options the allowed option long-tags * @return array the given options and there values * @access private */ function _parseArgs($argv, $short_options, $long_options = null) { if (!is_array($argv) && $argv !== null) { $argv = preg_split('/\s+/', $argv); } return Console_Getopt::getopt2($argv, $short_options); } /** * Output errors with PHP trigger_error(). You can silence the errors * with prefixing a "@" sign to the function call: @System::mkdir(..); * * @param mixed $error a PEAR error or a string with the error message * @return bool false * @access private */ function raiseError($error) { if (PEAR::isError($error)) { $error = $error->getMessage(); } trigger_error($error, E_USER_WARNING); return false; } /** * Creates a nested array representing the structure of a directory * * System::_dirToStruct('dir1', 0) => * Array * ( * [dirs] => Array * ( * [0] => dir1 * ) * * [files] => Array * ( * [0] => dir1/file2 * [1] => dir1/file3 * ) * ) * @param string $sPath Name of the directory * @param integer $maxinst max. deep of the lookup * @param integer $aktinst starting deep of the lookup * @return array the structure of the dir * @access private */ function _dirToStruct($sPath, $maxinst, $aktinst = 0) { $struct = array('dirs' => array(), 'files' => array()); if (($dir = @opendir($sPath)) === false) { System::raiseError("Could not open dir $sPath"); return $struct; // XXX could not open error } $struct['dirs'][] = $sPath; // XXX don't add if '.' or '..' ? $list = array(); while ($file = readdir($dir)) { if ($file != '.' && $file != '..') { $list[] = $file; } } closedir($dir); sort($list); if ($aktinst < $maxinst || $maxinst == 0) { foreach($list as $val) { $path = $sPath . DIRECTORY_SEPARATOR . $val; if (is_dir($path)) { $tmp = System::_dirToStruct($path, $maxinst, $aktinst+1); $struct = array_merge_recursive($tmp, $struct); } else { $struct['files'][] = $path; } } } return $struct; } /** * Creates a nested array representing the structure of a directory and files * * @param array $files Array listing files and dirs * @return array * @see System::_dirToStruct() */ function _multipleToStruct($files) { $struct = array('dirs' => array(), 'files' => array()); settype($files, 'array'); foreach ($files as $file) { if (is_dir($file)) { $tmp = System::_dirToStruct($file, 0); $struct = array_merge_recursive($tmp, $struct); } else { $struct['files'][] = $file; } } return $struct; } /** * The rm command for removing files. * Supports multiple files and dirs and also recursive deletes * * @param string $args the arguments for rm * @return mixed PEAR_Error or true for success * @access public */ function rm($args) { $opts = System::_parseArgs($args, 'rf'); // "f" do nothing but like it :-) if (PEAR::isError($opts)) { return System::raiseError($opts); } foreach($opts[0] as $opt) { if ($opt[0] == 'r') { $do_recursive = true; } } $ret = true; if (isset($do_recursive)) { $struct = System::_multipleToStruct($opts[1]); foreach($struct['files'] as $file) { if (!@unlink($file)) { $ret = false; } } foreach($struct['dirs'] as $dir) { if (!@rmdir($dir)) { $ret = false; } } } else { foreach ($opts[1] as $file) { $delete = (is_dir($file)) ? 'rmdir' : 'unlink'; if (!@$delete($file)) { $ret = false; } } } return $ret; } /** * Make directories. Note that we use call_user_func('mkdir') to avoid * a problem with ZE2 calling System::mkDir instead of the native PHP func. * * @param string $args the name of the director(y|ies) to create * @return bool True for success * @access public */ function mkDir($args) { $opts = System::_parseArgs($args, 'pm:'); if (PEAR::isError($opts)) { return System::raiseError($opts); } $mode = 0777; // default mode foreach($opts[0] as $opt) { if ($opt[0] == 'p') { $create_parents = true; } elseif($opt[0] == 'm') { // if the mode is clearly an octal number (starts with 0) // convert it to decimal if (strlen($opt[1]) && $opt[1]{0} == '0') { $opt[1] = octdec($opt[1]); } else { // convert to int $opt[1] += 0; } $mode = $opt[1]; } } $ret = true; if (isset($create_parents)) { foreach($opts[1] as $dir) { $dirstack = array(); while (!@is_dir($dir) && $dir != DIRECTORY_SEPARATOR) { array_unshift($dirstack, $dir); $dir = dirname($dir); } while ($newdir = array_shift($dirstack)) { if (!call_user_func('mkdir', $newdir, $mode)) { $ret = false; } } } } else { foreach($opts[1] as $dir) { if (!@is_dir($dir) && !call_user_func('mkdir', $dir, $mode)) { $ret = false; } } } return $ret; } /** * Concatenate files * * Usage: * 1) $var = System::cat('sample.txt test.txt'); * 2) System::cat('sample.txt test.txt > final.txt'); * 3) System::cat('sample.txt test.txt >> final.txt'); *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -