A simple example of the key member method used for copying elFinder Functions: Multi-file copy or move operations Note: In order to facilitate the test, some judgments are simply processed. In the directory where the program file is located Generate a new test folder as the destination folder. If it needs to be modified.
- ? /**
- * Simple copy/move operation example:> PHP5
- *
- */
- /**
- * Determine whether a file exists
- *
- */
- Function _ isFind ($ filename ){
- Return @ file_exists ($ filename );
- }
- /**
- * Determine whether a folder exists? Simple processing: only identify the root directory
- *
- */
- 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: the file name is used as the element value.
- * @ Paramstring destination folder
- * @ Paramstring operand: move-move; copy-copy
- * @ Return bool
- */
- Function _ copy_move ($ src = array (), $ dst = '', $ op = 'move '){
- If (! Is_array ($ src )){
- $ Src = array ($ src );
- }
- // Determine whether the source file exists?
- Foreach ($ src as $ val ){
- If (_ isFind ($ val) === FALSE ){
- Return _ log ('src file not find ', $ val );
- }
- }
- // Determine whether the destination folder exists? Generate if it does not exist
- // Simple processing: the actual application needs to be modified
- If (_ isFindDir ($ dst) === FALSE ){
- @ Mkdir ($ dst );
- }
- // Perform the move or copy operation
- Foreach ($ src as $ val ){
- $ _ Dst = $ dst. '/'. basename ($ val );
- // Determine whether the target file exists? Operations not 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-service ');
- }
- 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;
- }
- /**
- * Logging
- *
- */
- 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. you need to modify the $ src array parameter; 2. you can modify the third parameter of _ copy_move to test the move/copy operations respectively.
- *
- */
- $ Src = array ('IMG ', 'min', 'phpinfo. php ');
- $ Dst = 'test ';
- Var_dump (_ copy_move ($ src, $ dst, 'copy '));
- /* End of php */
|