FileGetContents: Difference between revisions
Jump to navigation
Jump to search
m (Portuguese version indexed) |
No edit summary |
||
Line 12: | Line 12: | ||
nil|string fileGetContents ( file theFile [ , bool verifyContents = true ] ) | nil|string fileGetContents ( file theFile [ , bool verifyContents = true ] ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{Added feature/item|1.6.1|1.6.0|22560| | |||
<syntaxhighlight lang="lua"> | |||
nil|string fileGetContents ( string filePath [ , bool verifyContents = true ] ) | |||
</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. | ||
{{Added feature/item|1.6.1|1.6.0|22560| | |||
*'''filePath:''' Path to a file you want to read | |||
}} | |||
*'''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 | ||
Line 22: | Line 33: | ||
==Example== | ==Example== | ||
<section name="Shared" class="both" show="true"> | |||
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 32: | Line 44: | ||
end | end | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</section> | |||
<section name="Shared" class="both" show="true"> | |||
This example reads content from a requested model and sends it back to the client. | |||
<syntaxhighlight lang="lua"> | |||
addEvent('onRequestModel', true) | |||
addEventHandler('onRequestModel', resourceRoot, function(path) | |||
triggerClientEvent(client, 'onSendModel', client, fileGetContents(path)) | |||
end) | |||
</syntaxhighlight> | |||
</section> | |||
==See Also== | ==See Also== | ||
{{File functions}} | {{File functions}} | ||
[[pt-br:fileGetContents]] | [[pt-br:fileGetContents]] |
Revision as of 16:23, 24 June 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
Click to collapse [-]
SharedThis 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)() end
Click to collapse [-]
SharedThis example reads content from a requested model and sends it back to the client.
addEvent('onRequestModel', true) addEventHandler('onRequestModel', resourceRoot, function(path) triggerClientEvent(client, 'onSendModel', client, fileGetContents(path)) end)