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
Syntax to write data to the file in Lua: file_name specifies the name of the file to be opened. mode is the mode in which the file must be opened. The file can be opened in read mode(r), write mode(w) or append mode(a). file_descriptor:write() operation is used to write the contents to the file.
I/O library is used for reading and manipulating files in Lua. There are two kinds of file operations in Lua namely implicit file descriptors and explicit file descriptors.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With