Hasimbola
Store generated file inside created path
Hello , I have this code
def get_image do
{:ok, query} = Repo.query("SELECT photolink FROM produits") for c <- query.rows do {:ok, photo} = Enum.fetch(c, 0) File.write("/tmp/#{photo}", photo, [:binary]) endend
I want to store the file generated from File.write inside my project such as inside “/priv/uploads/file_name”
I am able to store the file inside /tmp but I have problem when I try to store the file inside another path
Please, can someone help me, thanks a lot
Thanks! ![]()
Marked As Solved
egze
"/priv/uploads/#{photo}" is an absolute path and doesn’t exist on your hard drive.
Look at this thread: Accessing the priv folder from Mix release - #2 by NobbZ
What you need is something like this:
"#{List.to_string(:code.priv_dir(:your_app_name))}/uploads/#{photo}"
or use a relative path.
Also Liked
LostKobrakai
This can be done simpler:
Application.app_dir(:your_app_name, Path.join("priv/uploads", photo))
NobbZ
In general you do not want to write to priv! You should consider it as a read only artifact store.
If you need to write to a persistent location, you should manage it aside to your application, and backup it seperately.
In general the priv folder is part of your release, if you really change its content, you invalidate the integrity of your release.
Files are stored in a separete section of your filesystem or in an S3 bucket or whatever file storage backend you use, and as such has to be considered state of your application similar to your PostgreSQL database.
LostKobrakai
What is the problem?
Hasimbola
The program doesn’t found the path
I want to change
File.write(“/tmp/#{photo}”, photo, [:binary])
to something like this :
File.write(“/priv/uploads/#{photo}”, photo, [:binary])
but the path is not found , this return :
[{:error, :enoent}]
LostKobrakai
Are you sure parent folders of that path exist?








