php 如何使用PHP将文件从一个目录复制到另一个目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5772769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:26:24  来源:igfitidea点击:

How to copy a file from one directory to another using PHP?

phpfilefile-iocopyfilesystems

提问by Click Upvote

Say I've got a file test.phpin foodirectory as well as bar. How can I replace bar/test.phpwith foo/test.phpusing PHP? I'm on Windows XP, a cross platform solution would be great but windows preferred.

假设我test.phpfoo目录中有一个文件以及bar. 我怎样才能bar/test.phpfoo/test.phpusing替换PHP?我使用的是 Windows XP,跨平台解决方案会很棒,但首选 Windows。

回答by Pascal MARTIN

You could use the copy()function :

您可以使用该copy()功能:

// Will copy foo/test.php to bar/test.php
// overwritting it if necessary
copy('foo/test.php', 'bar/test.php');


Quoting a couple of relevant sentences from its manual page :


引用其手册页中的几个相关句子:

Makes a copy of the file source to dest.

If the destination file already exists, it will be overwritten.

将文件源复制到 dest。

如果目标文件已经存在,它将被覆盖。

回答by Dizzi

You could use the rename()function :

您可以使用rename()函数:

rename('foo/test.php', 'bar/test.php');

This however will movethe file not copy

然而,这将移动文件而不是复制

回答by cweinberger

copywill do this. Please check the php-manual. Simple Google search should answer your last two questions ;)

副本将做到这一点。请检查php-manual。简单的 Google 搜索应该可以回答您的最后两个问题;)

回答by Mukesh Jakhar

You can copy and past this will help you

你可以复制和过去这对你有帮助

<?php
$file = '/test1/example.txt';
$newfile = '/test2/example.txt';
if(!copy($file,$newfile)){
    echo "failed to copy $file";
}
else{
    echo "copied $file into $newfile\n";
}
?>

回答by Yogendra - eCommerce Developer

Best way to copy all files from one folder to another using PHP

使用 PHP 将所有文件从一个文件夹复制到另一个文件夹的最佳方法

<?php
$src = "/home/www/example.com/source/folders/123456";  // source folder or file
$dest = "/home/www/example.com/test/123456";   // destination folder or file        

shell_exec("cp -r $src $dest");

echo "<H2>Copy files completed!</H2>"; //output when done
?>

回答by Makhi Ngubane

Hi guys wanted to also add on how to copy using a dynamic copying and pasting.

嗨,伙计们还想添加如何使用动态复制和粘贴进行复制。

let say we don't know the actual folder the user will create but we know in that folder we need files to be copied to, to activate some function like delete, update, views etc.

假设我们不知道用户将创建的实际文件夹,但我们知道在该文件夹中我们需要将文件复制到,以激活某些功能,如删除、更新、视图等。

you can use something like this... I used this code in one of the complex project which I am currently busy on. i just build it myself because all answers i got on the internet was giving me an error.

你可以使用这样的东西......我在我目前正在忙的一个复杂项目中使用了这段代码。我只是自己构建它,因为我在互联网上得到的所有答案都给了我一个错误。

    $dirPath1 = "users/$uniqueID"; #creating main folder and where $uniqueID will be called by a database when a user login.
    $result = mkdir($dirPath1, 0755);
            $dirPath2 = "users/$uniqueID/profile"; #sub folder
            $result = mkdir($dirPath2, 0755);
                $dirPath3 = "users/$uniqueID/images"; #sub folder 
                $result = mkdir($dirPath3, 0755);
                    $dirPath4 = "users/$uniqueID/uploads";#sub folder
                    $result = mkdir($dirPath4, 0755);
                    @copy('blank/dashboard.php', 'users/'.$uniqueID.'/dashboard.php');#from blank folder to dynamic user created folder
                    @copy('blank/views.php', 'users/'.$uniqueID.'/views.php'); #from blank folder to dynamic user created folder
                    @copy('blank/upload.php', 'users/'.$uniqueID.'/upload.php'); #from blank folder to dynamic user created folder
                    @copy('blank/delete.php', 'users/'.$uniqueID.'/delete.php'); #from blank folder to dynamic user created folder

I think facebook or twitter uses something like this to build every new user dashboard dynamic....

我认为 facebook 或 twitter 使用这样的东西来构建每个新用户仪表板动态....

回答by Eclipse

You can use both rename() and copy().

您可以同时使用 rename() 和 copy()。

I tend to prefer to use rename if I no longer require the source file to stay in its location.

如果我不再需要源文件留在其位置,我倾向于使用重命名。