TIL: Temporary data folder

The temporary directory for data storage is often used. It can be especially needed when the system prepares a file for sending, downloading, and processing data. The most common use of /tmp as the temporary directory is; however this can be dangerous!

Simply using the code below may not always work. You will feel it, mainly when you use releases or different operating systems.

1
2
  temp_file = Path.join("/tmp", file_name_variable)
  File.write!(temp_file, content)

Depending on the configuration, /tmp may be represented differently. But that’s not a problem for the Elixir language! Just make a small modification to fix this problem:

1
2
3
4
  temp_dir = System.tmp_dir!()

  temp_file = Path.join(temp_dir, file_name_variable)
  File.write!(temp_file, content)

System.tmp_dir!/0 may return different results, depending on environment variables and operating system. See the documentation for more details.

Get new posts and extra comments

You'll receive every new post with extra unpublished comments available only to the subscribers!

I won't send you spam. Unsubscribe at any time.