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

当用户注册他的用户名和密码时,我让 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 

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

C:/xampp/htdocs/users/用户名

现在,我想要这段代码做的是在创建该用户名的子文件夹后将 .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 插入没有任何作用。

【问题讨论】:

  • PATH_SEPARATOR 是一个分号 ; 也许你打算使用 DIRECTORY_SEPARATOR 代替?检查man page
  • 您是否有理由不为此使用数据库?特别是因为您似乎将用户凭据保存在文本文件中,任何人都可以访问?顺便说一句,sha1() 不是创建密码哈希的好函数。它又老又快,这意味着暴力破解它并不难。您也没有使用散列,这意味着很容易针对彩虹表运行散列。请改用 PHP 的 password_hash() 和 password_verify()。
  • 如果我是你,我可能也不会真正为用户创建真正的文件夹,而是使用一些路由器并根据 URL 获取正确的用户。如果您创建实际文件夹,则需要保护自己免受Path Traversal
  • “现在我们必须使用哈希” - 当然,但sha1() 是一种糟糕的密码哈希方法。这是不安全的。它甚至在手册中这样说“警告不建议使用此功能来保护密码,因为这种散列算法的速度很快。” 为什么不从一开始就使用password_hash()?我仍然觉得如果全部使用数据库,这一切都会更快更安全。谁告诉你必须先这样做?这看起来很疯狂。
  • 很公平。但是,如果是您的教授告诉您使用 sha1() 散列密码并将users.txt 可访问的 htdocs 文件夹下,我会争取让教授被解雇,如果我是你。教人们不良和不安全的习惯是……糟糕!无论如何,关于您的问题,这一行:file_put_contents($dir . $subdir . PATH_SEPARATOR . $filename); 实际上缺少第二个参数,即数据。 Here's the manual。如您所见,第二个参数不是可选的。检查您的服务器错误日志。

标签: php csv directory fopen fputcsv


【解决方案1】:

解决方法如下:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 2018-08-09
    • 1970-01-01
    • 2019-06-12
    • 2017-01-27
    • 2020-06-05
    • 1970-01-01
    相关资源
    最近更新 更多