Question

I've looked the LuaFileSystem doc and didn't really understood how I could create a temporary file and write in it. Also, I'm not sure exactly where I can find the temp file I create.. In /tmp?

Here's how my function would look like:

do
   function upload_file(web)

      f =  -- creates a temporary file
      f:write(file.contents)     -- writes the content of the file uploaded in the temp file
      f:seek("set", 0)          -- we go back at the beginning
      s = f:read("*a")          -- read it out
      print (s)                 -- print it out
      f:close()                 -- close it
   end
end
Was it helpful?

Solution

There are two solutions in standard Lua:

  • io.tmpfile, which returns a handle for a temporary file. This file is opened in update mode and it is automatically removed when the program ends.

  • os.tmpname, which returns a string with a file name that can be used for a temporary file. The file must be explicitly opened before its use and explicitly removed when no longer needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top