FileGetContents: Difference between revisions
Jump to navigation
Jump to search
(Created page with "__NOTOC__ {{Server client function}} Reads the entire contents of the file '''starting from the current file position''', optionally verifies the read contents by computing and comparing the checksum with the expected one, and returns the content as string. 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. Th...") |
No edit summary |
||
Line 2: | Line 2: | ||
{{Server client function}} | {{Server client function}} | ||
{{Added feature/item|1.6.1|1.6.0|21932| | |||
Reads the entire contents of the file '''starting from the current file position''', optionally verifies the read contents by computing and comparing the checksum with the expected one, and returns the content as string. | Reads the entire contents of the file '''starting from the current file position''', optionally verifies the read contents by computing and comparing the checksum with the expected one, and returns the content as string. | ||
}} | |||
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. | 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. |
Revision as of 19:18, 7 August 2023
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 file test.txt and outputs its contents to the console.
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)() end
See Also
- fileClose
- fileCopy
- fileCreate
- fileDelete
- fileExists
- fileFlush
- fileGetPath
- fileGetPos
- fileGetSize
- fileIsEOF
- fileOpen
- fileRead
- fileRename
- fileSetPos
- fileWrite