将文件复制到新目录(PHP)

时间:2015-09-07 21:58:46

标签: php file-put-contents

我先前问了一个类似的问题,但之前不知道怎么回事。下面我根据我从注册表单中获取的用户名创建了一个新目录。我只需要将模板文件包含在刚刚制作的新目录中。我得到的结果是将文件包含在一个级别的目录中。新目录是使用用户名创建的,但在其目录中不包含模板文件。我在这上花了几个小时,所以我再也不用打扰你了。 我做错了什么?

 $folder = DIRECTORY_SEPARATOR; // adds the forward slash
   $name = $user->username;   // included from a login script I purchased 
   $thisdir = "../associate"; // desired directory
   $folderPath = $thisdir . $folder . $name;
   $file = copy('../associate/joshua/career.php', $folderPath.'.php'); // copy this file into new directory
        if(!file_exists($folderPath)){
            mkdir($folderPath);
            chmod($folderPath,0777);
        }

     file_put_contents(realpath($folderPath) .'/'. $folderPath, $file);

    });

1 个答案:

答案 0 :(得分:1)

如果我理解你,你想将career.php模板从/ associates /文件夹复制到/ associates / username / folder

   $rootfolder = $_SERVER['DOCUMENT_ROOT'];
   $name = $user->username;  
   $thisdir = "/associate/";
   $folderPath = $rootfolder.$thisdir.$name."/"; // desired directory

if(!is_dir($folderPath)){
        mkdir($folderPath, 0777, true);
    }
$currentfile = $rootfolder.$thisdir.'joshua/career.php';
    $destination = $folderPath.'career.php';
  copy($currentfile, $destination); // copy this file into new directory