Skip to content

File Read/Write#


file_read#

The file_read function in MivaScript is a powerful tool for reading the contents of a file into a string variable. It allows developers to access and manipulate the content of files within Miva Merchant’s file system directly from the template language. Here’s some additional information about the file_read function:

Purpose: The primary purpose of file_read is to read the entire contents of a file and store it in a string variable for further processing. This function is commonly used when you need to work with the data contained within a file, such as CSV files, text files, configuration files, etc.

<mvt:assign name="g.file_read" value="file_read('FILE_PATH','VARIABLE_NAME', g.return_value)" />

Error Parameters#

Parameters Description
FILE_PATH The first parameter of the file_read function is the path to the file that you want to read. This can be an absolute path or a relative path to the file within the Miva Merchant file system.
VARIABLE_NAME The second parameter is the name of the variable where the content of the file will be stored. This variable will hold the entire content of the file as a single string.
RETURN_VALUE The file_read function does not return a value directly. Instead, it reads the contents of the specified file and stores them in the provided variable. If the file is successfully read, the function completes its task without returning an error. However, if the file cannot be read (e.g., due to permission issues or file not found), it may result in errors further downstream in the code.

Error Handling#

It’s essential to implement error handling mechanisms when using the file_read function. You should check whether the file was read successfully and handle any potential errors appropriately. For example, if the file path is incorrect or the file does not exist, the file_read function may fail, leading to unexpected behavior in your application.

File Access#

The file_read function allows you to access files within both the Data and Script directories of Miva Merchant. However, it’s crucial to ensure that you have the necessary permissions to read the specified file. Additionally, you should be mindful of the directory structure and specify the correct file path based on whether you’re reading from the data directory or the script directory.

Example#

<mvt:comment> Reading a CSV File  </mvt:comment>
<mvt:assign name="g.file_read" value="file_read('/credit.csv','script', g.data)" />
<mvt:if expr=" g.file_read NE -1 ">
    The file was read!
<mvt:else>
    There was an error reading the file
</mvt:if>

<mvt:comment> Output data in CSV to the page </mvt:comment>
<mvt:assign name="g.counter" value="1" />
<mvt:assign name="l.settings:line" value="gettoken( g.data, asciichar(10), g.counter )" />
<mvt:while expr="NOT ISNULL l.settings:line">
    <mvt:comment> Output individual CSV line to the page if it exists </mvt:comment>
    <mvt:if expr="'jsmith' CIN l.settings:line">
        <mvt:assign name="l.settings:credit:login"  value="gettoken( l.settings:line, asciichar(44), 1 ) " />
        <mvt:assign name="l.settings:credit:name"   value="gettoken( l.settings:line, asciichar(44), 2 ) " />
        <mvt:assign name="l.settings:credit:amount" value="gettoken( l.settings:line, asciichar(44), 3 ) " />
        Login:  &mvt:credit:login;<br>
        Name:   &mvt:credit:name;<br>
        Amount: &mvt:credit:amount;<br>
    </mvt:if>
    <mvt:assign name="g.counter" value="g.counter + 1" />
    <mvt:assign name="l.settings:line" value="gettoken( g.data, asciichar(10), g.counter )" />
</mvt:while>

file_create#

The file_create function in MivaScript is a valuable tool for creating new files within the file system of a Miva Merchant store. It enables developers to dynamically generate files and populate them with data directly from the template language. The function is commonly used when you need to generate files dynamically based on certain conditions or user input, such as exporting data to a CSV file, generating XML files, creating log files, etc.

Parameters#

Parameters Description
FILE_PATH The first parameter of the file_create function is the path to the file that you want to create. This can be an absolute path or a relative path within the Miva Merchant file system.
FILE_CONTENT The second parameter is the content that you want to write to the newly created file. This content can be a string containing text data, XML markup, CSV data, etc.
DIRECTORY_TYPE The third parameter specifies whether the file should be created in the Data directory or the Script directory of Miva Merchant. This parameter is optional and defaults to “Script” if not provided.
RETURN_VALUE The file_create function does not return a value directly. Instead, it creates a new file at the specified path and populates it with the provided content. If the file creation is successful, the function completes its task without returning an error. However, if there are issues with file permissions or directory structure, the function may fail, leading to potential errors further downstream in the code.

Error Handling#

It’s essential to implement error handling mechanisms when using the file_create function. You should check whether the file was created successfully and handle any potential errors appropriately. For example, if the file path is incorrect or the directory does not exist, the file_create function may fail, resulting in unexpected behavior in your application.

File Access#

The file_create function allows developers to create files within both the Data and Script directories of Miva Merchant. However, it’s crucial to ensure that you have the necessary permissions to create files in the specified directory. Additionally, you should be mindful of the directory structure and specify the correct file path based on your requirements.