Question regarding paths

Hi,

this may be a bit stupid beginner question, but I’m not really sure how to do it properly so here it is…

When working with files, I have an app folder and inside I have an uploads folder in which all uploads are stored. On my dev machine, to write a file to that folder I have to add a dot before ./uploads/FILENAME but when I put the app to the server, that dot is not necessary as the app folder is the root folder.

Now, am I doing something wrong here by even having this issue? Also, if this is normal, what would be the best way to handle those paths as they differ for local and production environment?

Only files from priv folder are copied after the build, the documentation can be found here:

priv - a directory that keeps all resources that are necessary in production but are not directly part of your source code. You typically keep database scripts, translation files, images, and more in here. Generated assets, created from files in the assets directory, are placed in priv/static/assets by default.

Now since the path will differ after releasing your application, you can get the priv path by:

:code.priv_dir(:my_app_name) |> List.to_string()
2 Likes

No it’s just a different environment. If you want you can set these up in your config so you can reference them to same in your codebase.

uploads_path = Application.get_env(:my_app, MyApp.Endpoint)[:uploads_path]

The config settings would look like:

prod → config :my_app, MyApp.Endpoint, uploads_path: “uploads”
dev → config :my_app, MyApp.Endpoint, uploads_path: “./uploads”

3 Likes