1

I am using mkdir() to create new files in my code, but for some reason it is setting something called "daemon" as the admin. I cannot delete these files, edit, access, or even move these files. How can I change this from happening when creating files in my code?

<?php
 $dir = 'myDir3';

 // owner will be the user/group the PHP script is run under
 if ( !file_exists($dir) ) {
     mkdir ($dir, 0777);
 }

 file_put_contents ($dir.'/test.txt', 'Hello File');


$file = 'template.php';

if(file_exists($file)){

    echo readfile($file);


    error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
    ini_set("display_errors", 1);

    copy($file, $dir.'/fake.php');



    

} else {

    echo 'file does not exist';

}





?>
Jofre
  • 3,718
  • 1
  • 23
  • 31
blueweimer
  • 23
  • 4

1 Answers1

0

Eureka Edit(1):

Ok, thanks again @blueweimer , for clarifying that you are using XAMPP on MacOS (and not XAMPP-vm); because thanks to that clarification I was able to find the right answer to your question, which can be found here.

To elaborate on the answer found in the link: By default, when creating any sort of folder or file in XAMPP in MacOS it uses the user and group found in the httpd.conf file (the d stands for "daemon", which is the name given to running processes in Unix systems (like Linux and Mac, Mac being a slant more differentiated from Unix but still carrying over some of the same terms)). So by changing the user and group found in this file you can change the user that keeps the server running. It's the user that Apache uses to access everything and anything inside XAMPP.

It's important to note that you are changing your access user when changing the file in httpd.conf; but that does not automagically change the owner of the files and folders that have already been created. According to the link above, these files are found in /Applications/XAMPP/xamppfiles/, you'd have to change the owner of the files and the folder itself to the new user you've changed it to in httpd.conf.

Do not change this user to your own, as any failure in security that can occur on XAMPP will allow anyone with access to your XAMPP server to access your user remotely.

Before Edit:

Firstly, if possible, please post a code snippet so that we may know how this is being accessed.

Secondly, I'd like to apologize for placing all of this in an answer(low reputation so I can't comment just yet), but judging by the mkdir manual page for php, it seems that it creates the directory with the user who's running it as the owner. Given that it's setting the owner to "daemon" and you are unable to access it, I believe it safe to assume that you are not establishing default permissions (0777) and are running the file as a daemon.

You could choose to change the permissions the folders are created with:

<?php
mkdir("/path/to/my/dir", 0777);
?>

Alternatively you could use the chown command to change the ownership from "daemon" to another user within the same php file you create the folders with:

<?php
mkdir("/path/to/my/dir", 0700);
chown("/path/to/my/dir", "username");
?>

This answer was given according to the information provided: you did not specify which webserver you're using (you've simply specified html), and you've not specified an OS, so my understanding ist that it is a Linux distribution (otherwise folder access would not be a problem with PHP but rather with the webserver's permissions).

fabc
  • 182
  • 10
  • 1
    I am using my Mac to do this and am running this on visual studio code. I will make sure to edit my question with the code snippet as soon as I submit this reply. I have changed permissions to 0777 to no avail. I was on with Apple Care for the majority of the day yesterday trying to figure out if this was an Apple problem but after uninstalling and reinstalling MAC OS, they informed me that it was not an issue with the Mac I'm using. I'm connected to local host using Xampp, I'm not sure if that clarifies anything but. – blueweimer Mar 29 '21 at 14:36
  • 1
    I can confirm that using chown() does not work. I just tried it in my code and it is still showing that the admin is daemon. – blueweimer Mar 29 '21 at 14:40
  • Ok, so if you're using XAMPP then you are using apache. I'll get back to you in a bit, as it's been a while since I've dealth with Apache, it requires its own little permissions when accessing and writing files. EDIT : Woah, as soon as you specified XAMPP I found another stackoverflow case that seems to deal with a similar issue to the one you are facing - https://stackoverflow.com/questions/9046977/xampp-permissions-on-mac-os-x - the folder XAMPP is using has it's own permissions exclusive to XAMPP, the folders are created inside this folder and inherit it's permissions. I'll keep searching – fabc Mar 29 '21 at 15:02
  • 1
    sorry for the late response, thank you for that information. I am having trouble figuring out exactly how to do this. Do I do this in terminal? What steps should I take to change this? – blueweimer Mar 29 '21 at 16:31
  • I'm afraid I'm going to have to out-do you on the late response, so no need to apologize. That being said, you can do this either graphically or in the terminal. As it can be a bit difficult to explain GUI navigation through text (and there's a character limit), I'll leave you with this link for changing folder permissions through GUI (https://support.apple.com/guide/mac-help/change-permissions-for-files-folders-or-disks-mchlp1203/mac). As for doing this through the terminal, it's a matter of heading to the parent directory, and running "chown -R" through the sudo command. I'm out of chars. – fabc Mar 30 '21 at 07:08
  • I guess I'll continue in this second comment: Running "chown -R" through the sudo command, so it'd be "sudo chown -R yourUser:yourGroup fileFolder", where "yourUser" is the user you want, ":" is used to denote the what follows is "yourGroup" (the group you want, only the user is required), the "-R" stands for recursive (everything inside the folder will also have it's owner changed), "sudo" lets you run the command as "root" (superuser, I believe it's called in MacOS), and "fileFolder" is the file or (in this case) folder who's permissions you're trying to change. Am I being clear? (no chars) – fabc Mar 30 '21 at 07:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230529/discussion-between-fabc-and-blueweimer). – fabc Mar 30 '21 at 07:18
  • Moved to chat (my messages were long and probably hard to read, will add a summary of everything as an addendum once the chat is over) – fabc Mar 30 '21 at 07:24