php实现复制移动文件的方法,
本文实例讲述了php实现复制移动文件的方法。分享给大家供大家参考。具体如下:
仿写elFinder的,抽出关键的成员方法做的一个简单示例
实现功能是:
实现多文件复制或移动操作
注:为了方便测试,对一些判断做了简单处理。并且会在程序文件所在目录下
新生成一个test文件夹作为目的文件夹。如果实际使用需要修改。
copy.php文件如下:
<?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 = '', $op = 'move') { if ( ! is_array($src)) { $src = array($src); } //判断源文件是否存在? foreach ($src as $val) { if ( _isFind($val) === FALSE) { return _log('Src file not find', $val); } } //判断目的文件夹是否存在? 如果不存在就生成 //简单处理: 实际应用需要修改 if (_isFindDir($dst) === FALSE) { @mkdir($dst); } //执行移动或复制操作 foreach ($src as $val) { $_dst = $dst.'/'.basename($val); //判断目的文件是否存在? 存在不允许进行操作 if (_isFind($_dst) === TRUE) { return _log('Dst file is exists', $dst); } else if (strpos($dst, $val) === 0) { return _log('Unable to copy/move into itself'); } if (strtolower($op) === 'move') { if ( ! rename($val, $_dst)) { return _log('Unable to move files', $val); } } else if (strtolower($op) === 'copy') { if ( ! _copy($val, $_dst)) { return _log('Unable to copy files', $val); } } } return 'Success!'; } /** * 复制操作 * */ function _copy($src, $dst) { if ( ! is_dir($src)) { if ( ! copy($src, $dst)) { return _log('Unable to copy files', $src); } } else { mkdir($dst); $ls = scandir($src); for ($i = 0; $i < count($ls); $i++) { if ($ls[$i] == '.' OR $ls[$i] == '..') continue; $_src = $src.'/'.$ls[$i]; $_dst = $dst.'/'.$ls[$i]; if ( is_dir($_src)) { if ( ! _copy($_src, $_dst)) { return _log('Unable to copy files', $_src); } } else { if ( ! copy($_src, $_dst)) { return _log('Unable to copy files', $_src); } } } } return TRUE; } /** * 日志记录 * */ function _log($msg, $arg = '') { if ($arg != '') { $msg = "date[".date('Y-m-d H:i:s')."]\tmsg[".$msg."]\targ[".$arg."]\n"; } else { $msg = "date[".date('Y-m-d H:i:s')."]\tmsg[".$msg."]\n"; } echo $msg; return @file_put_contents('copy.log', $msg, FILE_APPEND); } /** * 示例 * 1. 需要修改$src的数组参数 ; 2. 可以修改_copy_move的第三个参数以分别测试move/copy操作 * */ $src = array('img', 'min', 'phpinfo.php'); $dst = 'test'; var_dump(_copy_move($src, $dst, 'copy')); /*end of php*/
希望本文所述对大家的php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
