0

So I have this pretty straight foreword code for creating a dir with the name the user inputs, but it just won't create. The folder I am wanting to create it in is a subfoolder. When i test it locally it works just fine but then on the server there seems to be a problem. Can anyone help me ?

$title = str_replace(" ", "_", $_POST['title']);
if (!is_dir("uploads/".$title)){
        mkdir("uploads/".$title, 0777);

I guess it lies with the path ? But I just couldn't find out why.

Thanx guys

Chris

3
  • 1
    Do you have the appropriate permissions to create the folder? Commented Oct 30, 2014 at 11:08
  • Did you check the permissions of the directory you are trying to add this directory to? Commented Oct 30, 2014 at 11:09
  • Check your error logs, it will probably tell you why. For instance, it might be that the user the webserver runs as doesn't own the subfolder you try to create the directories in, nor is a group or other allowed to do so. (In that likely case: chown :the-group-of-the-webserver uploads && chmod g+rwx uploads). Commented Oct 30, 2014 at 11:10

2 Answers 2

1

For most hosting providers, you have to provide a full path, not a relative one:

$root = dirname(__FILE__); // or whatever what points to root dir

$target = $root.'/uploads/'.$title; // save to a variable, to not repeat

if (!is_dir($target) {
     mkdir($target, 0777);
}
Sign up to request clarification or add additional context in comments.

Comments

0

So after checking a few possibilities ( thanx for your help guys ) I realized, that my ftp program wasn't showing the upload folder correctly. Meaning when checking the upload folder via the host server interface the created folder did appear. But when I checked via my ftp program to see if the folder had been created, it wasn't there. So there must be some kind of problem that disables my ftp program from showing the created folder... I have no clue why this happens but nonetheless the code is working like so:

$target = "uploads/".$title; // save to a variable, to not repeat

if (!is_dir($target)) {
        mkdir($target);
        chmod($target, 0777);}

So thanx for the help guys.

Cheers Chris

1 Comment

mkdir() calls chmod() internally when you supply a 2nd argument

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.