Heroku - how to write into “tmp” directory?

久未见 提交于 2019-11-30 18:25:04

Is /tmp included in your git repo? Removed in your .slugignore? The directory may just not exist out on Heroku.

Try tossing in a quick mkdir before the write:

Dir.mkdir(File.join(Rails.root, 'tmp'))

Or even in an initializer or something...

Here's an elegant way

f = File.new("tmp/filename.txt", 'w')
f << "hi there"
f.close

Dir.entries(Dir.pwd.to_s + ("/tmp")) # See your newly created file in /tmp

Don't forget that whenever your app restarts (for any reason, including those outside your control), your files will be deleted, as they are only stored ephemerally.

Try it with heroku restart, you will see the new file you created is no longer there

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!