【问题标题】:Creating a subfolder in directory based off of Username then inserting a CSV file in that subfolder created在基于用户名的目录中创建一个子文件夹,然后在创建的子文件夹中插入一个 CSV 文件
【发布时间】:2018-04-23 00:21:27
【问题描述】:

当用户注册他的用户名和密码时,我让 php 工作,它将用户名和他/她的密码插入到 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 

            //EVERYTHING WORKS TILL HERE IS WHERE I GET STUCK

            $filename = 'books.csv';

            file_put_contents($dir . $subdir. $filename);


            // Print a message:
            print '<p>You are now registered!</p>';

        } else { // Couldn't write to the file.
            print '<p class="error">You could not be registered due to a system error.</p>';
        }

    } else { // Forgot a field.
        print '<p class="error">Please go back and try again!</p>'; 
    }

} else { // Display the form.




// Leave PHP and display the form:

    ?>

我尝试了其他方法,它会将 .csv 文件作为 username-books-.csv 插入到常规用户文件夹中,但由于某种原因,我无法在新创建的文件中创建 books.csv用户名子文件夹。

有什么建议吗?

【问题讨论】:

    标签: php csv subdirectory fputcsv


    【解决方案1】:

    您没有将/ 附加到您的路径中。

    $dir . $subdir 将是

    C:/xampp/htdocs/users/myusername.

    像你一样添加books.csv会导致

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

    解决方案:

    $filename = 'books.csv';
    file_put_contents($dir . $subdir . PATH_SEPARATOR . $filename); // Append the directory separator aswell
    

    请注意,您的帖子错过了 file_put_contents 的第二个参数 - 所以我在这个答案中也忽略了它,因为我不知道您在里面放了什么。但它不是可选的。

    【讨论】:

    • 好吧,这就是它之前所做的,因为我试过了。有 C:/xampp/htdocs/users/ 此代码创建一个新文件夹,例如,我注册我的名字,然后创建:C:/xampp/htdocs/users/Proximus。这就是我想要的,但我希望将一个名为 Books 的 .csv 文件插入到我创建的 Proximus 命名文件夹中,这样我就得到了 C:/xampp/htdocs/users/Proximus/books.csv。它现在在做什么,也就是您的代码所做的是 C:/xampp/htdocs/users/Proximusbooks.csv。在用户文件夹中创建一个名为 Proximusbooks.csv 的 csv 文件。
    • 您没有在我的答案中添加PATH_SEPARATOR 部分(这实际上是我添加到您的代码中的唯一部分)。
    • 我准确地输入了这个:$filename = 'books.csv'; file_put_contents($dir . $subdir . PATH_SEPARATOR . $filename);它在用户文件夹中创建了一个名为 Proximusbooks.csv 的文件,而不是将 books.csv 插入到 Proximus 文件夹中。 :(
    猜你喜欢
    • 1970-01-01
    • 2018-08-09
    • 2017-01-27
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 2013-03-13
    • 2013-06-21
    相关资源
    最近更新 更多