Home > Backend Development > PHP Tutorial > Copy Move Files - PHP

Copy Move Files - PHP

WBOY
Release: 2016-07-25 09:09:12
Original
944 people have browsed it
A simple example of imitating elFinder and extracting key member methods
The implemented function is:
Implement multiple file copy or move operations

Note: In order to facilitate testing, some judgments have been briefly processed. And it will be in the directory where the program file is located
Create a new test folder as the destination folder. Modification is required if actual use is required.
  1. ?/**
  2. * Simple example of copy/move operation: >PHP5
  3. *
  4. */
  5. /**
  6. * Determine whether the file exists
  7. *
  8. */
  9. function _isFind($filename) {
  10. return @file_exists($filename);
  11. }
  12. /* *
  13. * Determine whether the folder exists? Simple processing: only determine 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. * @param array Source folder array: Simple processing: use the file name as the element value
  27. * @param string Destination folder
  28. * @param string Operands: 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? If it does not exist, generate it
  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 whether the destination file exists? Operation is not allowed if it exists
  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. * 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. The array parameter of $src needs to be modified; 2. The third parameter of _copy_move can be modified to test the move/copy operation 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*/
Copy code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template