0

i am a newbie in the field of web development, i am trying to create a folder using php's mkdir() command...

when i use...

mkdir("somefolder");

it works...

i had tried

mkdir("/somefolder/".$id."/photos");

but it won't work....i think i am doing it wrong how do i do this...

well the $id is coming from the database so that folder for each user will be created

Any help will be greatly appriciated...

thank you in advance...

EDIT

mkdir($id);

does creates a folder but above mentioned doesn't....

if somefolder is already there.. then mkdir won't work....?

4 Answers 4

1

for this purpose recursive parameter needs to be true...

mkdir("/somefolder/$id/photos",0777,true)

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

Comments

1

$path = "/somefolder/$id/photos";

if (!file_exists( $path ) mkdir($path);

else

throw new Exception($path." file already exist");

Comments

0

Remove the single quotes around $id

mkdir("/somefolder/$id/photos");

Comments

0

This line is incorrect:

mkdir("/somefolder/'$id'/photos");

Instead it should be

mkdir("/somefolder/".$id."/photos");

or

mkdir("/somefolder/$id/photos");

Make sure that your www-data user (or whichever user your script runs under) has the rights to create folders in /somefolder (chown www-data /somefolder)

and please remember to check that $id has the correct format before to avoid access to other directories with $id = "../.."

You could use

if(!is_numeric($id)) {
    /* do something */
}

3 Comments

thanks for your reply Eric, if $id is numeric... then is it possible to create folder...
if it doesn't work give the rights to the www-data user or which ever user your server runs under. chown www-data /somefolder/
Maybe it's a problem with your absolute/relative path. Try removing the first slash. mkdir("somefolder/$id/photos")

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.