Copy move file-PHP

Source: Internet
Author: User
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.
  1. ? /**
  2. * Copy/move operation Simple example: >PHP5
  3. *
  4. */
  5. /**
  6. * Determine if the file exists
  7. *
  8. */
  9. function _isfind ($filename) {
  10. Return @file_exists ($filename);
  11. }
  12. /**
  13. * Determine if a folder exists? Simple processing: Only the root directory is judged
  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: Using filenames as element values
  27. * @paramstring Destination Folder
  28. * @paramstring operand: move-Mobile; copy-copying
  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 if 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 if the destination folder exists? Generates if it does not exist
  42. Simple processing: Actual application needs to be modified
  43. if (_isfinddir ($dst) = = = FALSE) {
  44. @mkdir ($DST);
  45. }
  46. Perform a move or copy operation
  47. foreach ($src as $val) {
  48. $_DST = $dst. ' /'. basename ($val);
  49. Determine if the destination file exists? There are no operations 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 ');
  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. * Log records
  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. 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
  112. *
  113. */
  114. $SRC = Array (' img ', ' min ', ' phpinfo.php ');
  115. $DST = ' Test ';
  116. Var_dump (_copy_move ($SRC, $DST, ' Copy '));
  117. /*end of php*/
Copy Code
  • 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.