Copy a mobile file-PHP

Source: Internet
Author: User
Copy a mobile file-PHP
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.

  1. ? /**
  2. * Simple copy/move operation example:> PHP5
  3. *
  4. */
  5. /**
  6. * Determine whether a file exists
  7. *
  8. */
  9. Function _ isFind ($ filename ){
  10. Return @ file_exists ($ filename );
  11. }
  12. /**
  13. * Determine whether a folder exists? Simple processing: only identify the root directory
  14. *
  15. */
  16. Function _ isFindDir ($ dir ){
  17. $ Ls = scandir (dirname (_ FILE __));
  18. Foreach ($ ls as $ val ){
  19. If ($ val = $ dir) return TRUE;
  20. }
  21. Return FALSE;
  22. }
  23. /**
  24. * Copy or move
  25. *
  26. * @ Paramarray source folder array: simple processing: the file name is used as the element value.
  27. * @ Paramstring destination folder
  28. * @ Paramstring operand: move-move; copy-copy
  29. * @ Return bool
  30. */
  31. Function _ copy_move ($ src = array (), $ dst = '', $ op = 'move '){
  32. If (! Is_array ($ src )){
  33. $ Src = array ($ src );
  34. }
  35. // Determine whether the source file exists?
  36. Foreach ($ src as $ val ){
  37. If (_ isFind ($ val) === FALSE ){
  38. Return _ log ('src file not find ', $ val );
  39. }
  40. }
  41. // Determine whether the destination folder exists? Generate if it does not exist
  42. // Simple processing: the actual application needs to be modified
  43. If (_ isFindDir ($ dst) === FALSE ){
  44. @ Mkdir ($ dst );
  45. }
  46. // Perform the move or copy operation
  47. Foreach ($ src as $ val ){
  48. $ _ Dst = $ dst. '/'. basename ($ val );
  49. // Determine whether the target file exists? Operations not allowed
  50. If (_ isFind ($ _ dst) === TRUE ){
  51. Return _ log ('dst file is exists', $ Dst );
  52. } Else if (strpos ($ dst, $ val) === 0 ){
  53. Return _ log ('unable to copy/move into itself-service ');
  54. }
  55. If (strtolower ($ op) === 'move '){
  56. If (! Rename ($ val, $ _ dst )){
  57. Return _ log ('unable to move files', $ val );
  58. }
  59. } Else if (strtolower ($ op) === 'copy '){
  60. If (! _ Copy ($ val, $ _ dst )){
  61. Return _ log ('unable to copy files', $ val );
  62. }
  63. }
  64. }
  65. Return 'Success! ';
  66. }
  67. /**
  68. * Copy operation
  69. *
  70. */
  71. Function _ copy ($ src, $ dst ){
  72. If (! Is_dir ($ src )){
  73. If (! Copy ($ src, $ dst )){
  74. Return _ log ('unable to copy files', $ src );
  75. }
  76. } Else {
  77. Mkdir ($ dst );
  78. $ Ls = scandir ($ src );
  79. For ($ I = 0; $ I <count ($ ls); $ I ++ ){
  80. If ($ ls [$ I] = '.' OR $ ls [$ I] = '..') continue;
  81. $ _ Src = $ src. '/'. $ ls [$ I];
  82. $ _ Dst = $ dst. '/'. $ ls [$ I];
  83. If (is_dir ($ _ src )){
  84. If (! _ Copy ($ _ src, $ _ dst )){
  85. Return _ log ('unable to copy files', $ _ src );
  86. }
  87. } Else {
  88. If (! Copy ($ _ src, $ _ dst )){
  89. Return _ log ('unable to copy files', $ _ src );
  90. }
  91. }
  92. }
  93. }
  94. Return TRUE;
  95. }
  96. /**
  97. * Logging
  98. *
  99. */
  100. Function _ log ($ msg, $ arg = ''){
  101. If ($ arg! = ''){
  102. $ Msg = "date [". date ('Y-m-d H: I: s '). "] \ tmsg [". $ msg. "] \ targ [". $ arg. "] \ n ";
  103. } Else {
  104. $ Msg = "date [". date ('Y-m-d H: I: s'). "] \ tmsg [". $ msg. "] \ n ";
  105. }
  106. Echo $ msg;
  107. Return @ file_put_contents ('copy. log', $ msg, FILE_APPEND );
  108. }
  109. /**
  110. * Example
  111. * 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.
  112. *
  113. */
  114. $ Src = array ('IMG ', 'min', 'phpinfo. php ');
  115. $ Dst = 'test ';
  116. Var_dump (_ copy_move ($ src, $ dst, 'copy '));
  117. /* End of php */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.