Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将文件复制到新目录(PHP)_Php_File Put Contents - Fatal编程技术网

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

将文件复制到新目录(PHP),php,file-put-contents,Php,File Put Contents,我以前也问过类似的问题,但以前不知道怎么问。下面,我根据从注册表单中获取的用户名创建了一个新目录。我只需要将模板文件复制到刚刚创建的新目录中。我得到的结果是将该文件包含在上一级的目录中。新目录是使用用户名创建的,但其目录中不包含模板文件。我花了几个小时在这上面,这样我就不用再打扰你们了我做错了什么? $folder = DIRECTORY_SEPARATOR; // adds the forward slash $name = $user->username; // inclu

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

 $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);

    });

如果我理解您的意思,您希望将career.php模板从/associates/folder复制到/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

你应该仔细阅读你的代码,因为你正在做的事情根本无法工作。你是删除了前面的低值问题还是打开了两个类似的问题?你尝试写了两次文件,第一次尝试复制它,然后尝试放置内容,但你传递了一个bool作为文件内容。你真的需要阅读你正在使用的功能的手册。“我做错了什么?”-我有一个调试功能。但这并不意味着我完全理解。我只学了三天PHP。很抱歉,弗雷德。那没用,安迪。它确实创建了目录,但它不包含文件。您必须确保您要复制的文件“carre.php”存在于/associate/folder中,只是添加了一个小编辑。我添加了正确的文件夹路径。“joshua/associate”,并将$destination移动到正确的位置。复制函数中有$folderPath,而不是创建的$destination变量。非常感谢你,安迪。你的工作是一个很大的帮助和解释。欢迎你@JoshuaAdams我已经更新了你注意到的打字错误。