0

Im pretty sure I simply dont have permission. Thank you for your answers anyway! I will switch to self hosting so I KNOW i have permission!

(I would delete this but it says I cannot b/c there are answers)

11
  • 3
    maybe the folder already exist, you want to check that first? Commented Jul 25, 2012 at 19:47
  • it doesnt! Ive tried with diff usernames, and each time it doesnt exist Commented Jul 25, 2012 at 19:47
  • Looks like you should add a check around the mkdir if the directory exists, or perhaps suppress the errors/warnings Commented Jul 25, 2012 at 19:47
  • I have it output the error just so i can figure it out. the server is .htaccess password protected till this error is resolved Commented Jul 25, 2012 at 19:48
  • did you check the folder permission. Commented Jul 25, 2012 at 19:48

5 Answers 5

1

First of all, what is the actual value of $username? Have you verified that it's not empty?

Dealing with the filesystem like this can result in several different problems. I like to put in a lot of extra checks so if something fails I have an easier time knowing why. I also like to deal in absolute directory names where possible, so I don't run into problems with relative paths.

$filesDir = '/path/to/files';
if (!file_exists($filesDir) || !is_dir($filesDir)) {
    throw new Exception("Files directory $filesDir does not exist or is not a directory");

} else if (!is_writable($filesDir)) {
    throw new Exception("Files directory $filesDir is not writable");
}

if (empty($username)) {
    throw new Exception("Username is empty!");
}

$userDir = $filesDir . '/' . $username;

if (file_exists($userDir)) {
    // $userDir should be all ready to go; nothing to do.
    // You could put in checks here to make sure it's 
    // really a directory and it's writable, though.

} else if (!mkdir($userDir)) {
    throw new Exception("Creating user dir $userDir failed for unknown reasons.");
}

mkdir() has some really useful options for setting permissions and making folders multiple levels deep. Check out PHP's mkdir page if you haven't yet.

For security, make sure your exceptions aren't revealing system paths to the end user. You may want to remove the folder paths from your error messages when your code goes onto a public server. Or configure things so your exceptions get logged but not displayed on the web page.

Sign up to request clarification or add additional context in comments.

Comments

0

This is the algorithm for this kind of scenario.

  1. Check if the folder exists.
  2. If the folder exists name the folder to something else or add a random number to it.
  3. Create the new folder.

    http://pastebin.com/VefbrzRS

Comments

0

Try this.

mkdir ("./files/$username");

Comments

0

Aren't those the same folder? files/Alex and files/Alex/ are the same. Do you mean files/$username and files/$username/files ? you are doing the same dir twice so that's the error

1 Comment

go to command line and do mkdir files/test then do without changing directories do mkdir files/test/ you'll get the same error
0

If you're on Linux or MacOs, there is also another scenario which would consist in calling the mkdir function of your shell.

It'll look like :

system('mkdir -p yourdir/files/$username')

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.