Main Page | Modules | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

File-Example

Sections:

Info

The file lib is a non object oriented class but it supports many useful functions.

Example Code

The following code demonstrates how to use the class.

<?php
/*!***********************************************************************
 *************************************************************************
 * \file            file-sample.php
 *
 * \author          Kai Klenovsek
 *
 * \date            First Step: 2004-12-29
 *
 * \note <br>
 * \b THE \bBEER-WARE \bLICENSE <br>
 *   As long as you retain this notice you can do whatever you want with <br>
 *   this stuff. If we meet some day, and you think this stuff is worth it, <br>
 *   you can buy me a beer in return. <br>
 * 
 * \b NOTES: <br>
 *   
***************************************************************************
***************************************************************************/
  
// Include phplibex
require("../../phplibex.inc.php");

// Create new file object
$file = new file();

$filename = "test.txt";

$data  = "Time     |Date       |Value\n";
$data .= "08:01:00 |2005-01-01 |1\n";
$data .= "08:02:00 |2005-01-02 |2\n";
$data .= "08:03:00 |2005-01-03 |3\n";
$data .= "08:04:00 |2005-01-04 |4\n";
$data .= "08:05:00 |2005-01-05 |5\n";
$data .= "08:06:00 |2005-01-06 |6\n";
$data .= "08:07:00 |2005-01-07 |7\n";
$data .= "08:08:00 |2005-01-08 |8\n";
$data .= "08:09:00 |2005-01-09 |9\n";
$data .= "08:10:00 |2005-01-10 |10";


if ( $file->file_check($filename) );
    $file->file_delete($filename);

/* This command append data to a file. If the file does not exist, the 
 * function create it. */
$file->file_append( $filename, $data );

// Get the file size
echo "<b>File size in kByte: </b>".$file->file_getsize($filename)."<br>";
echo "<b>File size in Byte : </b>".$file->file_getsize_byte($filename)."<br>";
echo "<b>Number of lines: </b>".$file->file_nrlines($filename)."<br><br>";

// Get the file header
echo "<b>File header: </b>".$file->file_getheader($filename)."<br><br>";

// Get only one column from file
$column = $file->file_get_column( $filename, 1, "|" );

// Show colum 1
echo "<b>Column 1:</b><br>";
for ( $loop=0; $loop<=sizeof($column)-1; $loop++ )
    echo $column[$loop]."<br>";    

// Get only one column reverse from file
$column = $file->file_get_column_reverse( $filename, 1, "|" );

// Show colum 1
echo "<br><b>Column 1 reverse:</b><br>";
for ( $loop=0; $loop<=sizeof($column)-1; $loop++ )
    echo $column[$loop]."<br>";

// Echo complete data as string
echo "<br><b>Show complete data as string:</b><br>"; 
echo $file->file_getdata_normal($filename);

// Load complete data line by line into array
$data = $file->file_getdata($filename);

// Show array data
echo "<br><br><b>Show complete data as array:</b><br>";
for ( $loop=0; $loop<=sizeof($data)-1; $loop++ )
    echo $data[$loop]."<br>";    

?>

Generated on Fri Aug 26 08:13:54 2005 for PHP-Lib-Xtreme by  doxygen 1.4.3