FileGetContents: Difference between revisions
Jump to navigation
Jump to search
(Undo revision 79654 by Fernando187 (talk)) Tag: Undo |
CrosRoad95 (talk | contribs) No edit summary |
||
(2 intermediate revisions by one other user not shown) | |||
Line 3: | Line 3: | ||
{{Added feature/item|1.6.1|1.6.0|21938| | {{Added feature/item|1.6.1|1.6.0|21938| | ||
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. | 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]]. | ||
}} | }} | ||
Line 12: | Line 12: | ||
nil|string fileGetContents ( file theFile [ , bool verifyContents = true ] ) | nil|string fileGetContents ( file theFile [ , bool verifyContents = true ] ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{OOP||[[file]]:getContents}} | {{OOP||[[file]]:getContents}} | ||
===Required Arguments=== | ===Required Arguments=== | ||
*'''theFile:''' A handle to the file you wish to get the contents from. Use [[fileOpen]] to obtain this handle. | *'''theFile:''' A handle to the file you wish to get the contents from. Use [[fileOpen]] to obtain this handle. | ||
*'''verifyContents:''' Set to true, to compare the computed and the expected checksum of the file content. | |||
*'''verifyContents:''' Set to true, to compare the computed and the expected checksum of the file content | |||
===Returns=== | ===Returns=== | ||
Line 33: | Line 22: | ||
==Example== | ==Example== | ||
This example opens the code.lua file, checks its contents, and then runs it. | This example opens the code.lua file, checks its contents, and then runs it. | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
Line 41: | Line 29: | ||
if buffer then | if buffer then | ||
loadstring(buffer)() | 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 | end | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==See Also== | ==See Also== | ||
{{File functions}} | {{File functions}} | ||
[[pt-br:fileGetContents]] | [[pt-br:fileGetContents]] |
Latest revision as of 05:24, 19 October 2024
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.
Syntax
nil|string fileGetContents ( file theFile [ , bool verifyContents = true ] )
OOP Syntax Help! I don't understand this!
- Method: file:getContents(...)
Required Arguments
- theFile: A handle to the file you wish to get the contents from. Use fileOpen to obtain this handle.
- verifyContents: Set to true, to compare the computed and the expected checksum of the file content.
Returns
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.
Example
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