LiveView can't wait for ``, and shows {:error, :enoent} randomly

Hi, before showing a image in my LiveView page, I want to check if this image exist or not
for this reason, I used File.exists?() and, Although the image is in the preset direction but it shows me true or false randomly

this behavior destroys my conditions

if @form.value != nil and file_exist(@form.value) do


  def file_exist(file) do
    Path.join(["apps/mishka_html/priv/static/", file])
    |> File.exists?()

how can I fix this ?


Are you in your application changing your working dir?

Also perhaps it’s better to use :code priv_dir/1 instead.


no, I don’t Change my directory, and I print address in each request that is right

Path.join([:code.priv_dir(:mishka_html), "static", image]) |> File.exists?()

I think it can help me, why this has problem with direct address?

By “address” do you mean the absolute path or the relative one? And again, using :code.priv_dir/1 or Application.app_dir/2 will be much more reliable in general.

Using relative pathes will break as soon as you start the application from a different location than its src root.


absolute path, exists in my system !! like


This is a relative path, not absolute.

Have you tried using :code.priv_dir/1 or Application.app_dir/2 already, they are the ultimative solution to your problem.


yes, but not in this project thanks, it works now


I have a question, if this file exists out of my project directory like,


what is your suggestion ? I think :code.priv_dir/1 or Application.app_dir/2 just cover project directory, yes ?

Be very careful with passing form data to filesystem lookups. You don’t want someone to be able to pass “…/…/config/prod.exs” as a form input and have that show your production configuration and potentially secret tokens.


That looks like a configurable base dir for look ups. Which you then use with Path.absname/1,2.

Make sure to check if the resulting path is indeed within the base dir to avoid the security issues José mentioned.