search
Homephp教程php手册php移动文件夹和文件程序代码

php中移动复制文件我们使用copy,下面我们来看一个简单的实例,他实现了目录与目录文件移动,希望此文章对大家帮助。

方法一,copy+unlink方式

我的思路是:移动=新建+删除。在移动之前在目标的目录新建一个文件夹,然后在把文件和目录都复制过去,最后在执行删除.

<?php
/**
 * @parammoveDir  剪切文件和目录
 * @param string $to 目标文件路径
 * @param string $from 源文件路径
 */
function moveDir($from, $to) {
    if (!is_dir($from)) { //判断$from源文件目录是不是存在
        return false;
    }
    $from = rtrim(str_replace(&#39;//&#39;, &#39;/&#39;, $from) , &#39;/&#39;); //为了在linux上兼容,我们把/符号全部转换成/因为windows下面2个符号都可以的.
    $files = scandir($from); //列出源文件目录的文件和文件夹,并且以数组的形式存入 $files。
    /*
    $files的输出结果:
    Array ( [0] => . [1] => .. [2] => a [3] => b [4] => c [5] => dir [6] => dir.php [7] => dir2 [8] => dir2.php [9] => function_file.php [10] => homework.php )
    可以看到scandir这个函数会输出2个多余的值: [0] => . [1] => ..在这对我们木有用.写一个if干掉他们。
    */
    foreach ($files as $file) { //对 $files数组进行遍历,方便对数组里面的文件夹和文件进行复制和删除.
        if (in_array($file, array(
            &#39;.&#39;,
            &#39;..&#39;
        ))) { // array(&#39;.&#39;,&#39;..&#39;)新建了一个只有.和..的数组,并且查找$file里面知否有.和..这2个值
            continue;
        }
        $subFrom = $from . &#39;/&#39; . $file; //把遍历后的文件夹或者文件名变成一个新的路径
        $subTo = $to . &#39;/&#39; . $file;
        if (is_dir($subFrom)) {
            @mkdir($subTo); //判断$subFrom是不是一个目录,如果是目录就在目标的目录下面,新建一个目录
            moveDir($subFrom, $subTo); //递归执行新建目录。
            
        } else { //不是目录的话,就直接复制文件.复制完就删除文件.
            copy($subFrom, $subTo);
            unlink($subFrom); //删除所有文件
            
        }
        @rmdir($subFrom); //删除所有的目录
        
    }
    return true;
}
?>
moveDir(&#39;C:/Users/Administrator/Desktop/0704&#39;,&#39;e:&#39;);//在这传递你要移动的文件或者目录的地址

方法二,rename

1.对于文件,rename可以在不同盘符之间移动.

2.对于空文件夹,rename也可以在不同盘符之间移动.但是目标文件夹的父目录必须存在.

3.对于非空文件夹,只能在同一盘符下移动.不过,1和3,应该差不多能够对付所有的应用情况了.


<?php
rename("D:/testdir/test", "F:/totestdir/mydir");
?>

对于一个40M的文件,copy+unlink方式需要7.6249899864197秒,而rename方式,只需要0.024738788604736,快300倍.

<?php
//定义一个变量,保存文件名
$file = "html/cache.txt";
$rename = "html/renameCache.txt";
//使用rename()函数重命名一个文件
if (rename($file, $rename) == TRUE) {
    echo "重命名文件成功!";
} else {
    echo "重命名文件失败!";
}
//使用rename()函数移动文件,并改名
rename("html/renameCache.txt", "html/a/2.txt");
//使用rename()函数重命名目录
rename("html", "cache");
//使用rename()函数移动目录到目标目录
rename("b", "cache/b");
?>


文章链接:

随便收藏,请保留本文地址!

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
Java ArrayList遍历时使用foreach和iterator删除元素的区别是什么?Java ArrayList遍历时使用foreach和iterator删除元素的区别是什么?Apr 27, 2023 pm 03:40 PM

一、Iterator和foreach的区别多态差别(foreach底层就是Iterator)Iterator是一个接口类型,他不关心集合或者数组的类型;for和foreach都需要先知道集合的类型,甚至是集合内元素的类型;1.为啥说foreach底层就是Iterator编写的代码:反编译代码:二、foreach与iterator时remove的区别先来看阿里java开发手册但1的时候不会报错,2的时候就会报错(java.util.ConcurrentModificationException)首

php如何判断foreach循环到第几个php如何判断foreach循环到第几个Jul 10, 2023 pm 02:18 PM

​php判断foreach循环到第几个的步骤:1、创建一个“$fruits”的数组;2、创建一个计数器变量“$counter”初始值为0;3、使用“foreach”循环遍历数组,并在循环体中增加计数器变量的值,再输出每个元素和它们的索引;4、在“foreach”循环体外输出计数器变量的值,以确认循环到了第几个元素。

PHP返回一个键值翻转后的数组PHP返回一个键值翻转后的数组Mar 21, 2024 pm 02:10 PM

这篇文章将为大家详细讲解有关PHP返回一个键值翻转后的数组,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。PHP键值翻转数组键值翻转是一种对数组进行的操作,它将数组中的键和值进行交换,生成一个新的数组,其中原始键作为值,原始值作为键。实现方法在php中,可以通过以下方法对数组进行键值翻转:array_flip()函数:array_flip()函数专门用于键值翻转操作。它接收一个数组作为参数,并返回一个新的数组,其中键和值已交换。$original_array=[

PHP返回数组中的当前元素PHP返回数组中的当前元素Mar 21, 2024 pm 12:36 PM

这篇文章将为大家详细讲解有关PHP返回数组中的当前元素,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。获取PHP数组中的当前元素php为访问和操作数组提供了多种方法,其中包括获取数组中的当前元素。以下介绍几种常用的技术:1.current()函数current()函数返回数组内部指针当前指向的元素。指针最初指向数组的第一个元素。使用以下语法:$currentElement=current($array);2.key()函数key()函数返回数组内部指针当前指向元

foreach和for循环的区别是什么foreach和for循环的区别是什么Jan 05, 2023 pm 04:26 PM

区别:1、for通过索引来循环遍历每一个数据元素,而forEach通过JS底层程序来循环遍历数组的数据元素;2、for可以通过break关键词来终止循环的执行,而forEach不可以;3、for可以通过控制循环变量的数值来控制循环的执行,而forEach不行;4、for在循环外可以调用循环变量,而forEach在循环外不能调用循环变量;5、for的执行效率要高于forEach。

如何使用forEach函数遍历对象的属性?如何使用forEach函数遍历对象的属性?Nov 18, 2023 pm 06:10 PM

如何使用forEach函数遍历对象的属性?在JavaScript中,我们经常需要对对象的属性进行遍历操作。如果你想使用一种简洁的方法来遍历对象的属性,forEach函数是一个非常好的选择。在本文中,我们将介绍如何使用forEach函数来遍历对象的属性,并提供具体的代码示例。首先,让我们来了解一下forEach函数的基本用法。forEach函数是Java

php foreach循环中怎么获取当前元素的索引值php foreach循环中怎么获取当前元素的索引值Mar 23, 2023 am 09:17 AM

在PHP中,foreach语句被广泛用于对数组和对象进行遍历。在进行循环的过程中,我们有时需要获取当前循环到第几个元素。本文将为大家介绍怎样在PHP foreach循环中获取当前元素的索引值。

PHP中break关键字的作用和用法详解PHP中break关键字的作用和用法详解Jun 28, 2023 pm 06:39 PM

PHP中break关键字的作用和用法详解在PHP编程中,break是一种控制流语句,用于中断当前循环或switch语句,并跳出该循环或switch。本文将详细介绍break关键字的作用和用法。一、循环中的break在循环结构中,break的作用是提前终止循环,跳出循环体执行循环后的代码。常见的循环结构有for、while和do...while。在for循环中

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

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.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

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),