fileGetContents
Reads the entire contents of the file, optionally verifies the read contents by computing and comparing the checksum with the expected one, and returns the content as string. The file cursor position is not modified by calls to this function. File must be added in the meta.xml.
Please note that even if you enable SD #22 and #23 on your server, you are not protected from user attacks, which can happen after verification of the file, but before you read the contents of such verified file. This function enables you to safely read the contents of a meta.xml-listed file on both client and server.
OOP Syntax Help! I don't understand this!
- Method: file:getContents(...)
Syntax
nil|string fileGetContents ( file theFile, [ bool verifyContents = true ] )
Required Arguments
- theFile: A handle to the file you wish to get the contents from. Use [[fileOpen]] to obtain this handle.
Optional Arguments
NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use.
- verifyContents (default: true): Set to true, to compare the computed and the expected checksum of the file content.
Returns
- nil|string: result
Returns the bytes that were read from the file, but only if verification was disabled or if the checksum comparison succeeded. On failure, this function returns nil.
Code Examples
This example opens the code.lua file, checks its contents, and then runs it.
local handle = fileOpen("code.lua", true)local buffer = fileGetContents(handle) -- code.lua must be listed in meta.xml (for example as <file> for this example)fileClose(handle)
if buffer then loadstring(buffer)() -- This is just an example. You should avoid using loadstring. If you are dealing with configuration use json functions instead for security reasons.end