Start a conversation

Reading file with PHP

The code described below, open and read values from an external file to scriptcase.

An example would be a TXT file with values or texts. The tutorial will be based on the use of an control application, the codes can be called within the event onLoad or onValidade of the application.


The code is divided into the following steps:

  • Check the existence of the file path to be opened.
  • Open a file with extension TXT for example, using the command fopen ().
  • Make a loop to scan the file until its end using the command fgets ().
  • Check the return of fgets ().
  • Record the return of fgets () in the database using the macro sc_exec_sql ().
  • Close the file using the command fclose ().

1 - Check the existence of the file path to be opened.It is recommended to record the path and file name variables.

Example Code:

$path = "./doc"
$arquivo="documento.txt";$file = $path."/".$arquivo;

To verify the existence of the file to be opened, use the php function "file_exists ()"

Exemple Code:

if (!file_exists($file))
echo "arquivo nao existe";
else { 

}

  

2 - Open a file with extension TXT for example, using the command fopen ().The fopen () function is used for both recording as reading files. In our example, the function receive two parameters:

  • Full path to the file.
  • Char that tells what kind of file access.
Mode
Description
"r"
Read-only mode; Pointer is placed at the beginning of the file.
"r "
Reading and writing mode; Pointer is placed at the beginning of the file.
"w"
Write-only mode; Pointer is placed at the beginning of the file and everything within the file is erased. If the file does not exist, the system will try to create it.
"w "
Reading and writing mode; Pointer is placed at the beginning of the file and everything within the file is erased. If the file does not exist, the system will try to create it.
"a"
Write-only mode; Pointer is placed at the end of the file.. If the file does not exist, the system will try to create it.
"a "
Reading and writing mode; Pointer is placed at the end of the file. If the file does not exist, the system will try to create it.
"x"
Creates and opens the file in Write-only mode; The pointer is placed at the beginning of the file. If file already exists, afopen() call will fail, returning FALSE and raising an E_WARNING level error. If the file does not exist, the system will try to create it. Is is just as like as setting the flags O_EXCL|O_CREAT for the calling of open(2).
"x "
Creates and opens the file in Reading and Writing mode; The pointer is placed at the beginning of the file. The pointer is placed at the beginning of the file. If file already exists, afopen() call will fail, returning FALSE and raising an E_WARNING level error. If the file does not exist, the system will try to create it. Is is just as like as setting the flags O_EXCL|O_CREAT for the calling of open(2).

 

Exemple Code:

$fp = fopen($file, "r");

http://php.net/manual/pt_BR/function.fopen.php

 

3 - Make a loop to scan the file until its end using the command fgets (). Using lookup while (), use the function feof () to check the end of the file and thus be able to loop.


Exemple Code:

while (!feof($fp)){
 $current_line = fgets ($fp);
}

http://php.net/manual/en/function.feof.php

4 - Check the return of fgets ().If you want to ignore return empty file, use the php empty ().


Exemple Code:

if(!empty($current_line)){
 
 }

http://php.net/manual/en/function.empty.php

 

 5 - Record the return of fgets () in the database using the macro sc_exec_sql ().

This macro allows execution of SQL commands, for which the user could constrain the circumstances in which they should be executed.


Exemple Code:

sc_exec_sql("INSERT INTO tabela(valor) VALUES ($current_line)");

http://downloads.scriptcase.com.br/downloads/v6/manual/en_us/webhelp/manual_mp/28-Macros/00-macros_sc.htm#sc_exec_sql

6 - Close the file using the command fclose ().It is always recommended to close the file after reading it.


Exemple Code:

fclose($fp); 

http://php.net/manual/en/function.fclose.php

Choose files or drag and drop files
Was this article helpful?
Yes
No
  1. Amanda Santos (Migrated deleted Agent)

  2. Posted
  3. Updated

Comments