0

My goal :
I want to create Registration Form, when the registration is success so I direct to 'user profile -> index page'.

Directory :
Registration page in : myproject/account/create.php
User profile -> index page in : myproject/profile/nameuser/index.php

Step How it work :
1. Fill registration form
2. Submit form
3. If fail, fill again until success
4. When success, go to folder profile, then create a unique new folder for new user (it mean user folder), then create index page in user folder, then bring each user to access their each index page.

My problem :
I am trying to using method mkdir() in PHP but it can't solve because permission is denied
If any configuration in Linux to solve, I don't know what is the way

My Code :

if (empty($err)) {
    if ($insert_stmt = $mysqli->prepare("INSERT INTO member (uname, mail, pwd) VALUES (?, ?, ?)")) {
        $insert_stmt->bind_param('sss', $uname, $mail, $pwd);
        if (! $insert_stmt->execute()) {
            header('Location: ../error.php?err=Registration failure: INSERT');
        }
    }   
    $directory = "../profile/$uname";
    if(!is_dir($directory)) {
        mkdir("$directory");
        touch('$directory/index.php');
    }
    header('Location: $directory . index.php');
}

Hope someone can help me to solve my problem.

2 Answers 2

3

before proceeding to create the directory try to change the permissions of the project folder to 755 or 777

chmod -R 755 myproject/

if its still not working try it with permission 777

chmod -R 777 myproject/

and also while creating directory with mkdir you should assign permissions simultaneously

mkdir("$directory", 0755);
Sign up to request clarification or add additional context in comments.

5 Comments

are you sure that if the user can't create directory, he will be able to change permission of the directory??
the permission which is being changed is of the project folder. Also i've edited the answer and added the new mkdir(); function with little modifications.
NO, you should always avoid the use of 777 on any project folder/website. Thats why i've written that you should use 755 permissions. The use of 777 is only in extreme case when you want your project to work & you want to debug it irrespective of any other things.
Okay, I should learn more about that. And thanks for your help and explaination.
You're welcome, here's a link from which you can learn about the file permissions in linux linuxcommand.org/lts0070.php
0

Usualy your server run on a user account different from your user account on the machine. Apache can read your file to send it to your client but it doesn'T have write permission over your files.

Options:

Option one, make you folder writable using http://php.net/manual/en/function.chmod.php but this can be a security issue and some host will serve a 500 (internal) error if they find any 777 folder in your user folder. One trick is to use chmod(0777), write the file and re chmod(0755 or 0644) after write. check linux permission if you need a lead on the 'code' https://www.linux.com/learn/understanding-linux-file-permissions. You don'T provide the right code to make it haappend for you but it would be something like

$perm=substr(sprintf('%o', fileperms('../profile/')), -4); //current perm

chmod ("../profile/", "0777"); //make the parent folder writable

mkdir("$directory"); 
touch('$directory/index.php');

chmod ("../profile/", $perm); // make it back to old perms.

Another option is to add you and apache to a group ad then give write permission to this group but that would be for another stack exchange site.

You could also create a folder and chown (change onwership) to apache, but again it has it's flaw as when you will want to write on it yourself.

NOTE: all solution to your probem are subject to the operating system file permission. and you need to under stand the difference bewteen your user, apache user and even some host are using a user for php.

1 Comment

when I edit into your code, it show : Warning: fileperms(): stat failed for ../ , Warning: chmod(): Operation not permitted in / , Warning: mkdir(): Permission denied in / , Warning: touch(): Unable to create file, and Warning: chmod(): Operation not permitted in /

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.