-1

I created a php function that allows me to add images to a folder within my web application project. Right now it only works when I manually set the permissions of the folder to write enabled. Unfortunately that is not exactly what I need, because when I push my code to my group members with git, they would still have to manually set the permissions of the folder to enable writing as well, in order for my pushed code to work. all my group members and I are each running this project locally via an apache server, and I believe the problem is that apache does not have access to write within any of the folders within the project unless the folders are somehow set to write enable for everyone. I feel like it's not feasible to tell all my group members to change their folder permissions just so my one function can write to a folder.

I am looking for a way to set file permissions within my php code, I have already tried php functions such as chmod and mkdir to set permissions, however it gives me errors saying that I don't have permission to use those functions, which I guess makes sense, but is there a possible work around?

cj32
  • 23
  • 6
  • 3
    It would be a good idea to [edit] the question to include the directory structure and some notes on the permissions for relevant places where this will be installed. Perhaps even a note about which user the code is being executed at by your teammates; are they all executing it using Apache? Is everyone using the same operating system? Which operating system? – Jason Aller Apr 01 '20 at 14:50

3 Answers3

3
$structure = 'path';
mkdir($structure, 0777, true)

or

$structure = 'path';
mkdir($structure)
chmod($structure, 0777);
habib
  • 463
  • 3
  • 16
  • **to avoid permission error run those commad to your application path** `chown -R www-data:www-data /path/to/app` `chmod -R g+rw /path/to/app` – habib Apr 01 '20 at 16:14
  • Is this a command that would be run in the terminal, and wouldn't all my group members also have to run this command on their own terminal after i have pushed my code? If this is the case, then this doesn't solve my problem. – cj32 Apr 01 '20 at 16:51
  • 1
    @cj32 Your apache user `www-data` has to have access to the directory in order for the code that it is running to chmod that directory. There is no magic in the world to change that fact. Your group members will need to properly set up their environment to build and test and one of those steps will be properly setting permissions (just as one of those steps was to install apache and php). If there were a way around this it would be a security hole, right? – JNevill Apr 02 '20 at 14:00
  • 1
    @cj32 One other option might be to toss this whole thing in a container so you can put all of this prerequisite set up stuff (installation of web server, php, cfg/ini, file system, permissions, etc) in a dockerfile. That's obviously a pretty huge change, but it allows you to push/propogate environment changes. In the end though, you will still have to perform the steps outlines in this answer. – JNevill Apr 02 '20 at 14:05
0
<?php $result = mkdir ("/path/to/directory", "0777");
 ?>

Try this it will working

-1

Try using this (if shell_exec() is allowed):

(list of issues: only works on linux, has a security issue with potential solutions at bottom of post)

<?php
function readFile($file) { return shell_exec("cat $file"); }
function writeFile($file,$cont) { return shell_exec("echo $cont > $file"); }
function makeFile($filename) { return shell_exec("touch $filename"); }
function makeFolder($filename) { return shell_exec("mkdir -pv $filename"); }
// the -pv was used so you could do makeFolder("1/2/3/4")
function appendFile($file,$cont) { fileWrite($file,readFile($file).$cont); }
function appendToTopOfFile($file,$cont) { fileWrite($file,$cont.readFile($file);); }
?>

Use escapeshellcmd or escapeshellargs to escape user input. ex.

$file=escapeshellcmd($_GET['file']);
echo readFile($file);
Mayank
  • 1
  • 5