0

I've created a file directory system. here there is a function to make the directories and I want to prevent users making directories in ../ ie up one folder therefore I've created an if statement with strpos to search for it. Here's the code:

<div class="FormElement">
  <form method="post">
    <input type="text" name="newFolder" id="newFolder" class="newFolder"/>
    <input type="submit" value="Create">
  </form>

  <?php
    $uniqueUserPath = $_SESSION['userPath'];
    $folderName = $_POST['newFolder'];
    $makeFolder = $uniqueUserPath . "/" . $folderName;
    // mkdir($uniqueUserPath . "/" . $folderName);

    if (strpos($folderName, "../") == true) {
      echo 'there is a slash.';
    } else {
      mkdir($uniqueUserPath . "/" . $folderName);
      echo 'there isnt a slash';
    }
  ?>
</div>

And if you type in there "../" it stil echo's there isn't a slash and more importantly it will start making the directories in a folder outside of the users folder.

Any help would be appreciated kind regards,

Sean Bright
  • 118,630
  • 17
  • 138
  • 146

1 Answers1

1

strpos($folderName, "../") == true needs to be strpos($folderName, "../") !== false

The reason is because if it finds a match it returns the character index of the match, (e.g. 5) which then gets evaluated to true because 5 == true is true.

It returns boolean false if there is no match so you should be looking for that.

drew010
  • 68,777
  • 11
  • 134
  • 162
  • if i wanted to prevent a user from typing in how would i do that? or at least to escape the string, of course without the mysql function. – Abdullah Haider Oct 19 '15 at 20:04
  • Check out [`strip_tags`](http://php.net/strip_tags) for that. It will also strip PHP tags. – drew010 Oct 19 '15 at 20:16