Search code examples
phpcsvdirectoryfopenfputcsv

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


I have the php working for when a user registers his user name and password it takes the user name and his/her password and inserts it into the users.txt file. I also got it to work to create a subfolder with that users name within the general users folder. What i want it to do is to create a books.csv file and put that into the subfolder that was just created after the users name.

This is what i have so far that i have i tried but it does not work:

<?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 great here. A user registers with a user name, this then creates a folder within the users folder like

C:/xampp/htdocs/users/username

Now, what i want this code to do is to after making that subfolder of that users name to insert a .csv file into that new username sub folder so the end result will look like this

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

I tried using this:

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

and what this does is generate a .csv file called usernamebooks.csv so it looks like this

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

Example. I register myself and this happens:

C:/xampp/htdocs/users/Proximus/ <-- thats new folder created (great)

when i use the file_put_contents it just does this

C:/xampp/htdocs/users/Proximusbooks.csv it creates a .csv called "proximusbooks" in the users folder. I want a "books.csv" to be inserted within the proximus folder so it looks like this:

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

I also tried:

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

That just created a folder but did nothing with the .csv insertion.


Solution

  • THe solution is as follows:

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

    The other way of using

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

    Was giving me permission denies to write into the folder.