A simple example of writing elfinder, extracting key member methods The implementation features are: Implement multi-file copy or move operations Note: In order to facilitate the test, some judgments have been made simple treatment. And in the directory where the program files are located Freshmen into a test folder as the destination folder. If the actual use needs to be modified.
- ? /**
- * Copy/move operation Simple example: >PHP5
- *
- */
- /**
- * Determine if the file exists
- *
- */
- function _isfind ($filename) {
- Return @file_exists ($filename);
- }
- /**
- * Determine if a folder exists? Simple processing: Only the root directory is judged
- *
- */
- function _isfinddir ($dir) {
- $ls = Scandir (dirname (__file__));
- foreach ($ls as $val) {
- if ($val = = $dir) return TRUE;
- }
- return FALSE;
- }
- /**
- * Copy or move
- *
- * @paramarray source folder array: Simple processing: Using filenames as element values
- * @paramstring Destination Folder
- * @paramstring operand: move-Mobile; copy-copying
- * @return BOOL
- */
- function _copy_move ($src = Array (), $dst = ' ', $op = ' move ') {
- if (! Is_array ($SRC)) {
- $SRC = Array ($SRC);
- }
- Determine if the source file exists?
- foreach ($src as $val) {
- if (_isfind ($val) = = = FALSE) {
- Return _log (' SRC File not find ', $val);
- }
- }
- Determine if the destination folder exists? Generates if it does not exist
- Simple processing: Actual application needs to be modified
- if (_isfinddir ($dst) = = = FALSE) {
- @mkdir ($DST);
- }
- Perform a move or copy operation
- foreach ($src as $val) {
- $_DST = $dst. ' /'. basename ($val);
- Determine if the destination file exists? There are no operations allowed
- if (_isfind ($_dst) = = = TRUE) {
- Return _log (' Dst file is exists ', $DST);
- } else if (Strpos ($DST, $val) = = = 0) {
- Return _log (' Unable to copy/move into itself ');
- }
- if (Strtolower ($op) = = = ' Move ') {
- if (! Rename ($val, $_dst)) {
- Return _log (' Unable to move files ', $val);
- }
- } else if (Strtolower ($op) = = = ' Copy ') {
- if (! _copy ($val, $_DST)) {
- Return _log (' Unable to copy files ', $val);
- }
- }
- }
- Return ' success! ';
- }
- /**
- * Copy operation
- *
- */
- function _copy ($SRC, $DST) {
- if (! Is_dir ($SRC)) {
- if (! Copy ($SRC, $DST)) {
- Return _log (' Unable to copy files ', $SRC);
- }
- } else {
- mkdir ($DST);
- $ls = Scandir ($SRC);
- for ($i = 0; $i < count ($ls); $i + +) {
- if ($ls [$i] = = '. ' OR $ls [$i] = = ' ... ') Continue
- $_SRC = $src. ' /'. $ls [$i];
- $_DST = $dst. ' /'. $ls [$i];
- if (Is_dir ($_SRC)) {
- if (! _copy ($_SRC, $_DST)) {
- Return _log (' Unable to copy files ', $_SRC);
- }
- } else {
- if (! Copy ($_SRC, $_DST)) {
- Return _log (' Unable to copy files ', $_SRC);
- }
- }
- }
- }
- return TRUE;
- }
- /**
- * Log records
- *
- */
- function _log ($msg, $arg = ") {
- if ($arg! = ") {
- $msg = "date[". Date (' y-m-d h:i:s '). "] \tmsg[". $msg."] \targ[". $arg."] \ n ";
- } else {
- $msg = "date[". Date (' y-m-d h:i:s '). "] \tmsg[". $msg."] \ n ";
- }
- Echo $msg;
- Return @file_put_contents (' Copy.log ', $msg, file_append);
- }
- /**
- * Example
- * 1. The array parameters of $SRC need to be modified; 2. You can modify the third parameter of the _copy_move to test the move/copy operation separately
- *
- */
- $SRC = Array (' img ', ' min ', ' phpinfo.php ');
- $DST = ' Test ';
- Var_dump (_copy_move ($SRC, $DST, ' Copy '));
- /*end of php*/
Copy Code |