search

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 if ($ls[$i] == '.' OR $ls[$i] == '..') continue;
  80. $_src = $src.'/'.$ls[$i];
  81. $_dst = $dst.'/'.$ls[$i];
  82. if ( is_dir($_src)) {
  83. if ( ! _copy($_src, $_dst)) {
  84. return _log('Unable to copy files', $_src);
  85. }
  86. } else {
  87. if ( ! copy($_src, $_dst)) {
  88. return _log('Unable to copy files', $_src);
  89. }
  90. }
  91. }
  92. }
  93. return TRUE;
  94. }
  95. /**
  96. * Logging
  97. *
  98. */
  99. function _log($msg, $arg = '') {
  100. if ($arg != '') {
  101. $msg = "date[" .date('Y-m-d H:i:s')."]tmsg[".$msg."]targ[".$arg."]n";
  102. } else {
  103. $msg = "date[".date ('Y-m-d H:i:s')."]tmsg[".$msg."]n";
  104. }
  105. echo $msg;
  106. return @file_put_contents('copy.log', $msg, FILE_APPEND);
  107. }
  108. /**
  109. * Example
  110. * 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
  111. *
  112. */
  113. $src = array('img', 'min', 'phpinfo.php');
  114. $dst = 'test';
  115. var_dump(_copy_move($src, $dst , 'copy'));
  116. /*end of php*/
Copy code


Statement
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.