根据用户名在目录中创建子文件夹,然后在创建的用户名子文件夹中插入 CSV 文件

Creating a subfolder in directory based off of Username then inserting a CSV file in that username-subfolder created

我有 php 工作,当用户注册他的用户名和密码时,它会获取用户名和 his/her 密码并将其插入到 users.txt 文件中。我还可以使用它在一般用户文件夹中创建一个具有该用户名的子文件夹。我想要它做的是创建一个 books.csv 文件并将其放入刚刚在用户名后创建的子文件夹中。

这是我到目前为止所拥有的,我已经尝试过但它不起作用:

<?php 

// Identify the directory and file to use:
$dir = 'C:/xampp/htdocs/users/';
$file = $dir . 'users.txt';



if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.

    $problem = FALSE; // No problems so far.

    // Check for each value...
    if (empty($_POST['username'])) {
        $problem = TRUE;
        print '<p class="error">Please enter a username!</p>';
    }   

    if (empty($_POST['password1'])) {
        $problem = TRUE;
        print '<p class="error">Please enter a password!</p>';
    }

    if ($_POST['password1'] != $_POST['password2']) {
        $problem = TRUE;
        print '<p class="error">Your password did not match your confirmed password!</p>';
    } 

    if (!$problem) { // If there weren't any problems...

        if (is_writable($file)) { // Open the file.

            // Create the data to be written:
            $subdir = $_POST['username']; // folder to be created after the user name 
            $data = $_POST['username'] . "\t" . sha1(trim($_POST['password1'])) . "\t" . $subdir . PHP_EOL; // data is users name encrypted password

            // Write the data:
            file_put_contents($file, $data, FILE_APPEND | LOCK_EX);

            // Create the directory:

            mkdir ($dir . $subdir); // making a directory within a directory of folder name of user name 

这里的一切都很好。用户使用用户名注册,然后在用户文件夹中创建一个文件夹,如

C:/xampp/htdocs/users/username

现在,我想要这段代码做的是在创建该用户名的子文件夹后将 .csv 文件插入到新的用户名子文件夹中,这样最终结果将如下所示

C:/xampp/htdocs/users/username/books.csv

我试过使用这个:

$filename = 'books.csv';
file_put_contents($dir . $subdir . PATH_SEPARATOR . $filename);

这会生成一个名为 usernamebooks.csv 的 .csv 文件 所以看起来像这样

C:/xampp/htdocs/users/usernamebooks.csv

例子。我自己注册并发生了这种情况:

C:/xampp/htdocs/users/Proximus/ <-- 这就是创建的新文件夹(很棒)

当我使用 file_put_contents 时,它就是这样做的

C:/xampp/htdocs/users/Proximusbooks.csv 它会在用户文件夹中创建一个名为 "proximusbooks" 的 .csv。我想在 proximus 文件夹中插入一个 "books.csv",所以它看起来像这样:

C:/xampp/htdocs/users/proximus/books.csv

我也试过:

$filename = $dir . $subdir . 'boosk.csv';
$fd = fopen($filename, "w+");
fputs($fd, $fielname);
fclose($fd);

这只是创建了一个文件夹,但对 .csv 插入没有任何作用。

解决方法如下:

        $file1 = 'books.csv';
        $filename = $dir . $subdir . DIRECTORY_SEPARATOR . $file1;
        $fd = fopen ($filename, "w+");
        fputs($fd, $filename);
        fclose($fd);

另一种使用方式

$filename = 'books.csv';
file_put_contents($dir . $subdir . DIRECTORY_SEPARATOR . $filename);

给我的权限被拒绝写入文件夹。