0

I can't make a new directory in my web server. I think my code is ok to create a directory. Can you tell me what is the error ?

$path = "https://wwww.domain.com/astuces/uploads/products/".$id;
if(!is_dir($path)){

    mkdir($path);   

    if(mkdir($path)){
        echo "mkdir is created successfully";   
    }else{
        echo "directory is not created";
        echo mysql_error();
    }
}

It's always showing me "directory is not created".

5
  • 3
    Why are you running mkdir twice ? Commented Feb 20, 2014 at 7:46
  • You can't use mkdir with a URL as a parameter. try mkdir('/path/to/directory/'.$id,0755,true) Commented Feb 20, 2014 at 7:48
  • @AndyGee I'm trying it. Commented Feb 20, 2014 at 7:50
  • check permissions on folder Commented Feb 20, 2014 at 7:51
  • along with the answers, dont forget to check also the is_writeable() function. Commented Feb 20, 2014 at 7:53

2 Answers 2

3

This is not just a permission issue, you are doing impossible things.

$path = "https://wwww.domain.com/astuces/uploads/products/".$id;

You can never use a url as the path to create a directory in a server.

The path should be the path in your web server like:

$path = "/path/to/your/project/astuces/uploads/products/".$id;

And then make sure the apache user has the permission.

If the the parent directory also not exist at first, you have to set the third parameter to true of mkdir:

if(mkdir($path, 0755, true)){
Sign up to request clarification or add additional context in comments.

4 Comments

if I use : $path = "../../astuces/uploads" . DS . "products" . DS . $id; then it's allowable ?
where DS means a simple forward slash (/)
@Babu Only if you have the right path and right permission.
it's now showing me.."mkdir is created successfully". that's mean it's created but I can't see any directory. Now I just set my path look like this : $path = '../../astuces/uploads/products/'.$id;
0

Try this code

$path = "https://wwww.domain.com/astuces/uploads/products/".$id; if(!is_dir($path)){

$old_umask = umask(0);
mkdir($path, true);  
chmod($path, 0777);
umask($old_umask); 

if(mkdir($path)){
    echo "mkdir is created successfully";   
}else{
    echo "directory is not created";
    echo mysql_error();
}

}

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.