PHP implements the method of copying and moving files,
This article explains how PHP implements copying and moving files. Share to everyone for your reference. Specific as follows:
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.
The copy.php file is as follows:
<?php/** * 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 * @param array of source folders: Simple processing: Use file name as element value * @param string destination folder * @param string operand: Move-Mobile; Copy-copy * @return bool */function _copy_move ($src = Array (), $DST = ", $op = ' move ') {if (! Is_array ($SRC)) {$SR c = 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? Generate//simple processing if not present: The actual application needs to modify 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? Presence is not allowed to operate 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 (' Unabl E 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; /** * LogRecord * */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. The third parameter of the _copy_move can be modified to test the move/copy operation individually * */$src = array (' img ', ' min ', ' phpinfo.php '); $dst = ' Test '; Var_dump (_copy_move ($SRC, $DST, ' Copy '); *end of php*/
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/1039181.html www.bkjia.com true http://www.bkjia.com/PHPjc/1039181.html techarticle PHP Implementation of the method of copying and moving files, this article describes the PHP implementation of the method of copying and moving files. Share to everyone for your reference. Specific as follows: Write Elfinder, pull out the key ...