【发布时间】: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