1

I have this PHP script that is supposed to create an article when you fill out a form. I go the code from various sources combined but it does not seem to work. The code does not report any errors but when I try to go to http://somesite.com/article/article-name it gives me a 404

Code:

if (isset($_POST['title']) && isset($_POST['content'])) {
    if(!file_exists(dirname('/article/' . str_replace(' ', '-', $_POST['title'])))) {
        mkdir(dirname('/article/' . str_replace(' ', '-', $_POST['title'])), 0777, true);
        touch('/article/' . str_replace(' ', '-', $_POST['title']) . '/index.php');
        file_put_contents('/article/' . str_replace(' ', '-', $_POST['title']) . '/index.php', $_POST['content']);
    }
}

Any ideas on what is going wrong? All help appreciated!

  • Welcome to Stack Overflow! Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details. Please [edit] these details in or we may not be able to help. – Blue Jun 12 '17 at 00:48
  • @FrankerZ I edited it, I hope that helps. –  Jun 12 '17 at 00:53
  • 404 seems to indicate that it's not getting to your script properly. Perhaps a mod rewrite issue? – Blue Jun 12 '17 at 00:56

1 Answers1

0

OK guys. I made the script work by getting rid of all the dirname's!

Final Code:

if (isset($_POST['title']) && isset($_POST['content'])) {
    if(!file_exists(str_replace(' ', '-', $_POST['title']))) {
        mkdir(str_replace(' ', '-', $_POST['title']), 0777, true);
        touch(str_replace(' ', '-', $_POST['title']) . '/index.php');
        file_put_contents(str_replace(' ', '-', $_POST['title']) . '/index.php', $_POST['content']);
        echo "article made!";
    } else {
        echo "article not made! " . str_replace(' ', '-', $_POST['title']);
    }
} else {
    echo "article not made! " . str_replace(' ', '-', $_POST['title']);
}