Andri Sul
Andri Sul

Reputation: 505

PHP - Cannot to create new folder and new file because linux permission

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.

Upvotes: 0

Views: 1825

Answers (2)

Cool Breeze
Cool Breeze

Reputation: 955

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);

Upvotes: 3

Louis Loudog Trottier
Louis Loudog Trottier

Reputation: 1397

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.

Upvotes: 0

Related Questions