Take the 2-minute tour ×

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)

share|improve this question
maybe the folder already exist, you want to check that first? –  Ibu Jul 25 '12 at 19:47
it doesnt! Ive tried with diff usernames, and each time it doesnt exist –  Primm Jul 25 '12 at 19:47
Looks like you should add a check around the mkdir if the directory exists, or perhaps suppress the errors/warnings –  ernie Jul 25 '12 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 –  Primm Jul 25 '12 at 19:48
did you check the folder permission. –  pckabeer Jul 25 '12 at 19:48

5 Answers 5

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.

share|improve this answer

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.


share|improve this answer

Try this.

mkdir ("./files/$username");
share|improve this answer

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

share|improve this answer
Those are 2 different things i have tried –  Primm Jul 25 '12 at 20:00
go to command line and do mkdir files/test then do without changing directories do mkdir files/test/ you'll get the same error –  Alex Reynolds Jul 25 '12 at 20:02

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')
share|improve this answer

Your Answer


