unit icon indicating copy to clipboard operation
unit copied to clipboard

Issue with creating files and folders from PHP

Open elpado opened this issue 2 years ago • 2 comments

Hello dear developers,

I would like to test Unit for a small PHP project. I use the Docker Image with PHP 8.3 installed.

The code of the application is stored within the container under "/mnt/code/". But I have a, let's call it, data exchange directory. This directory lives outside of my application root.

-/
 -- /mnt
     -- /code
-/data-store  <-- exchange directory

My application need to create new folders and files in that exchange directory.

My config.json looks like this:

{
  "listeners": {
    "*:80": {
      "pass": "routes"
    }
  },

  "routes": [
    {
      "match": {
        "uri": "!/index.php"
      },
      "action": {
        "share": "/mnt/code/",
        "fallback": {
          "pass": "applications/exchange-data"
        }
      }
    }
  ],

  "applications": {
    "exchange-data": {
      "type": "php",
      "root": "/mnt/code/public/",
      "script": "index.php",
    }
  }
}

But when I try to create a new directory or file in the exchange folder, I receive a permission denied :(

    mkdir ('/data-store/data-set/2024-03-01', 0777, true);

    mkdir(): Permission denied in /mnt/code/src......

It's the very first time I'm trying to use Unit for one of my applications. I've also tried changing my configuration and adding a user/group - but to no success.

Is there something I have overlooked?

elpado avatar Mar 01 '24 14:03 elpado

You will need to ensure that the user that Unit is running as has permissions to create the directory. mkdir(2) needs write permission on the parent directory.

ac000 avatar Mar 01 '24 15:03 ac000

Thanks for the quick reply :)

I have now simply set up a small test container and tested it with the following configuration - and it now works. I am now trying to use the same configuration for the actual project and hope that there are no more problems.

Dockerfile

FROM unit:php8.3

WORKDIR /opt/app

RUN mkdir /data-store && chown -R unit:unit /data-store

COPY ./config.json /docker-entrypoint.d/config.json
COPY ./index.php /opt/app/public/index.php

config.json

{
  "listeners": {
    "*:80": {
      "pass": "routes"
    }
  },

  "routes": [
    {
      "match": {
        "uri": "!/index.php"
      },
      "action": {
        "share": "/opt/app/public$uri",
        "fallback": {
          "pass": "applications/data-exchange"
        }
      }
    }
  ],

  "applications": {
    "data-exchange": {
      "type": "php",
      "root": "/opt/app/public/",
      "script": "index.php",
      "user": "unit",
      "group": "unit"
    }
  }
}

index.php

<?php

$exchangeDirectory = '/data-store/' . date('Y-m-d');

echo sprintf('Check directory exists %s ... ', $exchangeDirectory);
if (!is_dir($exchangeDirectory)) {
    echo sprintf('not found - will create %s ... ', $exchangeDirectory);
    mkdir($exchangeDirectory, 0777, true);
}
echo 'done';

elpado avatar Mar 02 '24 13:03 elpado

Looks like you've got it sussed. Closing this now. Feel free re-open if required.

ac000 avatar Apr 09 '24 02:04 ac000