0

In PHP I am trying to make a new database type folder for different people on sign up and add files. I could easily create files and write to them but for some reason every time i try to have PHP create folders using the persons username variable as the path, all it does is create a folder named $username.

Here is my code cut down to the basics of that part.

<?php
$title = $_POST["title"];
$myFile = "/users/$username/title.txt";
$fh = fopen($myFile, 'w') or die("There was an error in changing your title.  <br />");
$stringData = "$title\n";
fwrite($fh, $stringData);
fclose($fh);

$template = $_POST["temp"];
$myFile = "$structure/template.txt";
$fh = fopen($myFile, 'w') or die("There was an error in changing your template.  <br />");
$stringData = "$template\n";
fwrite($fh, $stringData);
fclose($fh);
?>
5
  • Where is the code to make the directory? Commented Feb 23, 2012 at 12:58
  • 1
    Have you tried breaking out the $username from the string: $myFile = "/users/".$username."/title.txt"; Commented Feb 23, 2012 at 12:58
  • 2
    You should use mkdir() function before. fr2.php.net/manual/en/function.mkdir.php Commented Feb 23, 2012 at 12:58
  • can you please provide the values for $username and $structure? Commented Feb 23, 2012 at 12:59
  • I said it above, but it just creates the path /users/$username/title.txt it doesn't put in the actual username for the file path. I tried mkdir at one point but that just made a directory named $username also. Commented Feb 23, 2012 at 12:59

3 Answers 3

1

Try This

<?php
$title = $_POST["title"];
$myFile = "/users/".$username."/title.txt";
$fh = fopen($myFile, 'w') or die("There was an error in changing your title.  <br />");
$stringData = $title."\n";
fwrite($fh, $stringData);
fclose($fh);

$template = $_POST["temp"];
$myFile = $structure."/template.txt";
$fh = fopen($myFile, 'w') or die("There was an error in changing your template.  <br />");
$stringData = $template."\n";
fwrite($fh, $stringData);
fclose($fh);
?>
Sign up to request clarification or add additional context in comments.

Comments

0

To make this work, you do need to break out the variable (as Ben Griffiths mentioned) from the string and check it isn't empty. Also, make sure you do make the directory first created using mkdir() (aschuler mentioned this as well). The code, therefore, may look something like this but without knowing where $username, $structure, $title and $template come from, you may need to alter this a little:

<?php
$title = $_POST['title'];
if (trim($username) == '') {
    die("No username passed in!");
} else {
    $userdir = "/users".$username."/";
    mkdir($userdir);
    $fh = fopen($userdir."title.txt", 'w') or die("There was an error in changing your title.  <br />");

    $stringData = $title."\n";
    fwrite($fh, $stringData);
    fclose($fh);
}

$template = $_POST['temp'];
if (trim($template) == '') {
    die("No template passed in!");
} else {
    $structdir = $structure."/";
    mkdir($structdir);
    $fh = fopen($structdir."template.txt", 'w') or die("There was an error in changing your template.  <br />");

    $stringData = $template."\n";
    fwrite($fh, $stringData);
    fclose($fh);
}

?>

Hope this helps.

2 Comments

This should work, I can see that it should but for some reason even when I do this it creates the folder named $username or the code before that I used didn't create it at all. Do you think its my folder permissions?
Unlikely. I've ran the script on my server and while yes, I did have to change the permissions in the folder I was running the script in to 0755, when it did work, it created the directories with the $username variable correctly.
0

You're saying that this /users/$username/title.txt create exactly /users/$username/title.txt

so Your problem is you need to catch $username first, I don't know how your code look but maybe this?

<?php $username=$_SESSION['username']; //retrieve the username 
    //rest of your code 
    $myFile = "/users/$username/title.txt";
$fh = fopen($myFile, 'w') or die("There was an error in changing your title.  <br />");
$stringData = "$title\n";
fwrite($fh, $stringData);
fclose($fh);

$template = $_POST["temp"];
$myFile = "$structure/template.txt";
$fh = fopen($myFile, 'w') or die("There was an error in changing your template.  <br />");
$stringData = "$template\n";
fwrite($fh, $stringData);
fclose($fh);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.