Sunday, August 30, 2015

file reading || file handling in php

<?php
/*
 *for reading only
 * */
$pointer = fopen('php.txt', 'r'); //first argument is file name, 2nd parameter is mode some modes are r, w, a, a+
//fgets() return one line from file keep cursor to end of the line
echo fgets($pointer) ;
echo '<br>';
echo fgets($pointer) ;
echo '<br>';
fclose($pointer);



$pointer2 = fopen('php.txt', 'r');
//fgets() return one line from file keep cursor to end of the line
echo fgets($pointer2) ;
echo '<br>';
echo fread($pointer2, filesize('php.txt'));//this line echoing everything (all word) from php.txt
echo '<br>';
fclose($pointer2);


$pointer3 = fopen('php.txt', 'r');
while(!feof($pointer3)){
    echo fgetc($pointer3) . '<br>';
}
fclose($pointer3);
//feof() is file-end-of-file, fgetc() is file-get-character();

No comments:

Post a Comment

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }