1

I'm trying to create a nested directory on a web server. The code creates the directory $userID"."_blog_images ok, but not the post_"."$postID"."_images directory. I'm using a Mac. Thanks in advance.

<?php
//Create user root image directory if it does not exist
    if (!is_dir("$userID"."_blog_images")){
        mkdir("$userID"."_blog_images");
    }

//Create post image directory if it does not exist
    if (is_dir("$userID"."_blog_images")){
      if (!is_dir("$userID"."_blog_images/post_"."$postID"."_images")){
         mkdir("$userID"."_blog_images/post_"."$postID"."_images", 0777, true);
      }

    }
?>
4
  • Do you already have $userID"."_blog_images" as dir? Commented Jun 14, 2020 at 1:25
  • Yes that is created if it doesn't already exist. Commented Jun 14, 2020 at 1:42
  • Check out my answer. Commented Jun 14, 2020 at 1:48
  • Does PHP (specifically, the user that PHP is running under) have rights to create that subdirectory? Also, you may want to see why the call to mkdir() fails. Commented Jun 14, 2020 at 3:30

1 Answer 1

2

As per provided info in the comment section your you want to create a sub-dir in it when the parent dir is already present

So you code must be

<?php
//Create user root image directory if it does not exist
    if (!is_dir($userID."_blog_images/")){
        mkdir($userID."_blog_images");
    }

//Create post image directory if it does not exist
    if (is_dir($userID."_blog_images/")){
        mkdir($userID."_blog_images/post_".$postID."_images", 0777, true);
    }
?>
Sign up to request clarification or add additional context in comments.

12 Comments

I can see that I have confused the question as I didn't include the if statement if the sub directory doesn't exist. I do however want to check if the $userID"."blog_images exists before I create the sub directory post"."$postID"."_images in side of that. I will amend the code.
I have run the similar code on my pc and came to know that / is not present in the dir it doesn't works.So please check my edited code.
Thanks for your reply but I have edited the code as you suggested but it's not creating the sub directory.
If I use $userID_blog_images the underscores turn the variable $userID into $userID_blog_images variable.
So here what i meant to tell was $postID = "73/" and $userID = "292/"it should be like this
|

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.