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

php移动文件夹和文件程序代码

May 25, 2016 pm 04:52 PM
foreach

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;);//在这传递你要移动的文件或者目录的地址
Copy after login

方法二,rename

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

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

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


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

对于一个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");
?>
Copy after login


文章链接:

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

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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? Apr 27, 2023 pm 03:40 PM

1. The difference between Iterator and foreach is the polymorphic difference (the bottom layer of foreach is Iterator) Iterator is an interface type, it does not care about the type of collection or array; both for and foreach need to know the type of collection first, even the type of elements in the collection; 1. Why is it said that the bottom layer of foreach is the code written by Iterator: Decompiled code: 2. The difference between remove in foreach and iterator. First, look at the Alibaba Java Development Manual, but no error will be reported in case 1, and an error will be reported in case 2 (java. util.ConcurrentModificationException) first

How to determine the number of foreach loop in php How to determine the number of foreach loop in php Jul 10, 2023 pm 02:18 PM

​The steps for PHP to determine the number of the foreach loop: 1. Create an array of "$fruits"; 2. Create a counter variable "$counter" with an initial value of 0; 3. Use "foreach" to loop through the array, and Increase the value of the counter variable in the loop body, and then output each element and their index; 4. Output the value of the counter variable outside the "foreach" loop to confirm which element the loop reaches.

PHP returns an array with key values ​​flipped PHP returns an array with key values ​​flipped Mar 21, 2024 pm 02:10 PM

This article will explain in detail how PHP returns an array after key value flipping. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Key Value Flip Array Key value flip is an operation on an array that swaps the keys and values ​​in the array to generate a new array with the original key as the value and the original value as the key. Implementation method In PHP, you can perform key-value flipping of an array through the following methods: array_flip() function: The array_flip() function is specially used for key-value flipping operations. It receives an array as argument and returns a new array with the keys and values ​​swapped. $original_array=[

PHP returns the current element in an array PHP returns the current element in an array Mar 21, 2024 pm 12:36 PM

This article will explain in detail about the current element in the array returned by PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. Get the current element in a PHP array PHP provides a variety of methods for accessing and manipulating arrays, including getting the current element in an array. The following introduces several commonly used techniques: 1. current() function The current() function returns the element currently pointed to by the internal pointer of the array. The pointer initially points to the first element of the array. Use the following syntax: $currentElement=current($array);2.key() function key() function returns the array internal pointer currently pointing to the element

What is the difference between foreach and for loop What is the difference between foreach and for loop Jan 05, 2023 pm 04:26 PM

Difference: 1. for loops through each data element through the index, while forEach loops through the data elements of the array through the JS underlying program; 2. for can terminate the execution of the loop through the break keyword, but forEach cannot; 3. for can control the execution of the loop by controlling the value of the loop variable, but forEach cannot; 4. for can call loop variables outside the loop, but forEach cannot call loop variables outside the loop; 5. The execution efficiency of for is higher than forEach.

How to iterate through the properties of an object using the forEach function? How to iterate through the properties of an object using the forEach function? Nov 18, 2023 pm 06:10 PM

How to iterate through the properties of an object using the forEach function? In JavaScript, we often need to traverse the properties of objects. If you want to use a concise way to iterate over the properties of an object, the forEach function is a very good choice. In this article, we will explain how to use the forEach function to iterate over the properties of an object and provide specific code examples. First, let's understand the basic usage of the forEach function. forEach function is Java

How to get the index value of the current element in php foreach loop How to get the index value of the current element in php foreach loop Mar 23, 2023 am 09:17 AM

In PHP, the foreach statement is widely used to traverse arrays and objects. During the loop, we sometimes need to get the current element of the loop. This article will introduce how to get the index value of the current element in the PHP foreach loop.

Detailed explanation of the function and usage of break keyword in PHP Detailed explanation of the function and usage of break keyword in PHP Jun 28, 2023 pm 06:39 PM

Detailed explanation of the role and usage of the break keyword in PHP In PHP programming, break is a control flow statement used to interrupt the current loop or switch statement and jump out of the loop or switch. This article will introduce in detail the role and usage of the break keyword. 1. Break in a loop In a loop structure, the function of break is to terminate the loop early and jump out of the loop body to execute the code after the loop. Common loop structures include for, while and do...while. in for loop

See all articles