lua服务器读写文件,Openresty+Lua 读写文件

Talk is cheap. Show me the code.

因为 lua 写读写操作比较麻烦,所以大致封装了一下。

读文件:

-- 读文件

-- 参数:需要读取的文件路径

-- 返回值:读出的内容,读取错误。

-- 如果没有读出内容,第一个参数为 nil,否则第二个参数为 nil

local function read_file(file_name)

if not file_name then

return nil, "missing file_name"

end

local file = io.open(file_name,'r')

if not file then

return nil, "can\'t open file \"" .. file_name .. "\""

end

local content = file:read('*all')

file:close()

return content, nil

end

写文件:

-- 写文件

-- 参数:需要写入的文件路径,写入内容

-- 返回值:写入结果

-- 如果没有写入内容,返回错误内容,否则返回 nil

local function write_file(file_name, content)

if not file_name then

return nil, "m

你可能感兴趣的:(lua服务器读写文件)