1

I'm new to PHP and I'm trying to create a directory (for a user) and I get a 500 Error when the script is launched.

<html>
<head>
    <title>Login</title>
</head>
<body>
    <?php 
        chdir("users");
        mkdir($_POST["username"]);
    ?>
</body>

3
  • "A 500 error" could be caused by anything. If you get a 500 error, that means there is a more descriptive error message somewhere in your web server's error log. You need to look there to find out more. Commented Jul 7, 2015 at 21:23
  • A 500 Internal Error HTTP response usually means a syntax error in the PHP code. The exact error message can be found on your server in a file usually named php-errors.log but this exact location depends on the local configuration. The posted code looks valid, though. Commented Jul 7, 2015 at 22:44
  • Your $_POST may not be set, you should wrap it in an if(empty($_POST["username"]) { ... } I'd also recommend you escape the variable to void yourself of running any malicious code on the server, never trust user input! Commented Jul 7, 2015 at 22:47

4 Answers 4

1

first of all your problem can be caused by a huge amount of reasons so you need to narrow those down: turn error reporting ON.

Seccond, use the full, absolute path to create a directory so you don't get lost in your OS.

This way you can find out what the problem is, try this:

<html>
 <head>
  <title>Login</title>
 </head>
 <body>
  <?php 
    error_reporting(E_ALL);
    mkdir(/home/$_POST["username"]);
  ?>
 </body>
Sign up to request clarification or add additional context in comments.

Comments

0

Your error log will usually have a more detailed error message. I'd guess it's permissions though.

Make sure your web service is allowed to create directories in the "user" dir.

3 Comments

this should be posted as a comment, not an answer
I gave him what's likely to be the answer. Honestly I don't know why I'm taking exception to this particular comment as it happens on every goddamn stack post. idaf about the point system. I honestly only come on when things are slow at work. my response addresses his issue so I'm not sure why you think it should be a comment.
It was a permissions problem, thanks for your answer.
0

The most likely problem here is that th3 user folder doesn't have www-data as user or group, and then write permissions. You should lookup these things and set them if they aren't correct.

Comments

0

This is pretty bad practice to make a directory like that, but to answer your question you probably need to check and make sure you have that variable first, before you try to work with it. Like this.

if(isset($_POST["username"])){
        mkdir($_POST["username"]);
}

The reason being that variable in the post isn't always there, its only there when you populate it, typically by submitting a form

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.