Home > Backend Development > PHP Tutorial > Tips for copying and moving files in PHP

Tips for copying and moving files in PHP

墨辰丷
Release: 2023-03-30 16:28:02
Original
1723 people have browsed it

This article mainly introduces the techniques of copying and moving files in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

A simple example of imitating elFinder and extracting key member methods

The implemented function is:

Realize multiple file copy or move operations

Note: In order to facilitate testing, some judgments have been briefly processed. And a new test folder will be generated in the directory where the program file
is located as the destination folder. Modification is required if actual use is required. The
copy.php file is as follows:

<?php
/**
 * 复制/移动操作简单示例 : >PHP5
 *
 */
/**
 * 判断文件是否存在
 *
 */
function _isFind($filename) {
  return @file_exists($filename);
}
/**
 * 判断文件夹是否存在? 简单处理: 仅对根目录进行判断
 *
 */
function _isFindDir($dir) {
  $ls = scandir(dirname(__FILE__));
  foreach ($ls as $val) {
    if ($val == $dir) return TRUE;
  }
  return FALSE;
}
/**
 * 复制或移动
 *
 * @param  array  源文件夹数组: 简单处理:采用文件名作为元素值
 * @param  string 目的文件夹
 * @param  string 操作数: move - 移动 ; copy - 复制
 * @return bool
 */
function _copy_move($src = array(), $dst = &#39;&#39;, $op = &#39;move&#39;) {
  if ( ! is_array($src)) {
    $src = array($src);
  }
  //判断源文件是否存在?
  foreach ($src as $val) {
    if ( _isFind($val) === FALSE) {
      return _log(&#39;Src file not find&#39;, $val);
    }
  }
  //判断目的文件夹是否存在? 如果不存在就生成
  //简单处理: 实际应用需要修改
  if (_isFindDir($dst) === FALSE) {
    @mkdir($dst);
  }
  //执行移动或复制操作
  foreach ($src as $val) {
    $_dst = $dst.&#39;/&#39;.basename($val);
    //判断目的文件是否存在? 存在不允许进行操作
    if (_isFind($_dst) === TRUE) {
      return _log(&#39;Dst file is exists&#39;, $dst);
    } else if (strpos($dst, $val) === 0) {
      return _log(&#39;Unable to copy/move into itself&#39;);
    }
    if (strtolower($op) === &#39;move&#39;) {      
      if ( ! rename($val, $_dst)) {
        return _log(&#39;Unable to move files&#39;, $val);
      } 
    } else if (strtolower($op) === &#39;copy&#39;) {
      if ( ! _copy($val, $_dst)) {
        return _log(&#39;Unable to copy files&#39;, $val);
      }
    }
  }
  return &#39;Success!&#39;;
}
/**
 * 复制操作 
 *
 */
function _copy($src, $dst) {
  if ( ! is_dir($src)) {
    if ( ! copy($src, $dst)) {
      return _log(&#39;Unable to copy files&#39;, $src);
    }
  } else {
    mkdir($dst);
    $ls = scandir($src);
    for ($i = 0; $i < count($ls); $i++) { 
      if ($ls[$i] == &#39;.&#39; OR $ls[$i] == &#39;..&#39;) continue;
      $_src = $src.&#39;/&#39;.$ls[$i];
      $_dst = $dst.&#39;/&#39;.$ls[$i];
      if ( is_dir($_src)) {
        if ( ! _copy($_src, $_dst)) {
          return _log(&#39;Unable to copy files&#39;, $_src);
        }
      } else {
        if ( ! copy($_src, $_dst)) {
          return _log(&#39;Unable to copy files&#39;, $_src);
        }
      }
    }
  }
  return TRUE;
}
/**
 * 日志记录
 *
 */
function _log($msg, $arg = &#39;&#39;) {
  if ($arg != &#39;&#39;) {
    $msg = "date[".date(&#39;Y-m-d H:i:s&#39;)."]\tmsg[".$msg."]\targ[".$arg."]\n";
  } else {
    $msg = "date[".date(&#39;Y-m-d H:i:s&#39;)."]\tmsg[".$msg."]\n";
  }
  echo $msg;
  return @file_put_contents(&#39;copy.log&#39;, $msg, FILE_APPEND);
} 
/**
 * 示例
 * 1. 需要修改$src的数组参数 ; 2. 可以修改_copy_move的第三个参数以分别测试move/copy操作
 *
 */
$src = array(&#39;img&#39;, &#39;min&#39;, &#39;phpinfo.php&#39;);
$dst = &#39;test&#39;;
var_dump(_copy_move($src, $dst, &#39;copy&#39;));
/*end of php*/
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

How PHP accepts files and obtains suffix names

Testing methods and examples of PHP scripts

PHP method to integrate DISCUZ users

The above is the detailed content of Tips for copying and moving files in PHP. For more information, please follow other related articles on the PHP Chinese website!

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