0

pls i need help am trying to use the mkdir for user profile pic upload and its not working ... but when i use it normally i.e ordinarilly for example when i created a new php page and used " mkdir("newdir") "it worked

    <?php
if (isset($_FILES['profilepic'])) {

   if (((@$_FILES["profilepic"]["type"]=="image/jpeg") || 
 (@$_FILES["profilepic"]["type"]=="image/png") || (@$_FILES["profilepic"]
["type"]=="image/gif"))&&(@$_FILES["profilepic"]["size"] < 1048576)) { 
//1 Megabyte 
   $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$rand_dir_name = substr(str_shuffle($chars), 0, 15);
mkdir("userdata/profile_pics/$rand_dir_name");
}
else{

}

}
?>

<p>UPLOAD YOUR PROFILE PHOTO:</p> <br />

<form action="" method="POST" enctype="multipart/formdata">

<img src="./img/default_pic.png" width="70" />

<input type="file" name="profilepic" /><br /><br />

<input type="submit" name="uploadpic" value="Upload Photo"><br />

</form>
1
  • 1
    Please consider revising the code sample you posted in this question. As it currently stands, its formatting and scope make it hard for us to help you, here is a great resource to get you started on that. Commented Aug 30, 2015 at 12:54

3 Answers 3

1

its not because of php, the php code is written well the problem is in the html where the html form enctype is written wrong

this

<form action="" method="POST" enctype="multipart/formdata">

should be

<form action="" method="POST" enctype="multipart/form-data">
Sign up to request clarification or add additional context in comments.

Comments

1

Edit some code for your code with this:

Notice that it is best practise to use the variable outside of the string. Also you place the code in an if statement to check it executed correctly.

edit You also need to start the mkdir call with a / or even better start it with $_SERVER['document_root'] so you know you always get an absolute path to the directory you want to generate.

if(mkdir("/userdata/profile_pics/".$rand_dir_name)){
       print "this worked!";
}
else {
       print "This failed";
}

Also you need to check that the permissions for the parent directory are correct, that the PHP usergroup is allowed to create directories in the /profile_pics/ folder.

You can also check that PHP is in the correct working directory, you can check this with print gwtcwd();

Comments

-1

May be you should try like this (variable name along with a single-inverted comma)

mkdir("userdata/profile_pics/'$rand_dir_name'");

I hope it will work.

5 Comments

This is not the way to do this.
@Martin Well both ways are correct. There is no mistake in using that way. Maybe you didn't used php variables that way. But in php this is one other way to include variables in between string. I hope you will remove downvote after confirming it
The use of multiple quote marks reduces readability and adds needless encasing, as the variable is at the end of the string.
Oh man, i don't know how you treat this type of implementation. Seriously.
Why is this code not working? $dir = $GLOBALS['frontPath'].'/images/dir/'.$brand_id; $mkdir = mkdir($dir,0777); I get no error message.

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.