将文件传输到新目录

问题描述 投票:0回答:2

我只需要一个简单的代码即可将文件传输到具有变量名的新目录。

例如,这是我的代码的一部分,非常简单,请参见附件

如您所见,这是一个注册页面,创建用户时,会创建一个令牌,并使用用户名(在这种情况下为变量)创建一个文件夹,最后,它创建了该文件夹并最终发送了一个通过php mailer确认电子邮件。

我想要在用户注册后创建前面提到的文件夹后,它需要自动将位于temp / accounts / profile中的所有文件()复制到创建的新目录中,例如:

临时/帐户/个人资料/新用户

我尝试执行此操作,但是由于新目录依赖于用户名输入,因此它是一个变量,我不确定在用户注册后如何构建此查询以成功传输文件。enter image description here

php variables directory file-transfer user-registration
2个回答
0
投票

从一个目录中查找内容,并将其复制到新目录中。

$targetDir = 'temp/accounts/profile/newuser';
//of course create this folder first if it doesn't exist
mkdir($targetDir);

// Scan the directory and retrieve all files
// `array_diff` is to clean out directory thingies than can persist
foreach(array_diff(scandir($dirname), ['.','..']) as $val){

   copy("$dirname/$val", "$targetDir/$val");

}

0
投票

您需要找到所有文件,并一一复制。如果需要复制目录,则必须实现递归复制,在这种情况下,可以在循环中使用is_dir检查文件名是否为目录。

<?php
$source = 'temp/accounts/profile/newuser';
$destination = 'temp/accounts/profile/' . $name;
$files = glob($source . "/*.*"); //Find all files
foreach($files as $filename){
    copy($filename, $destination . "/" . basename($filename));
}
?>
© www.soinside.com 2019 - 2024. All rights reserved.