minte9
LearnRemember



fread

Unlike fgets(), it does not concern itself with newline characters.
 
$file = "file.txt";

if ($fd = fopen($file, "w+")) {
    fwrite($fd, "aaarn");
    fwrite($fd, "bbb");
    fclose($fd);
}

if ($fd = fopen($file, "r")) {
    
    $contents = fread($fd, filesize($file));
    echo $contents . "<br>"; // aaa bbb

    rewind($fd);
    echo fread($fd, 2) . "<br>"; // aa

    rewind($fd);
    echo fread($fd); 
        // Warning: fread() expects exactly 2 parameters, 1 given
}

Exists

Checks whether a file or directory exists.
 
if (!file_exists('paht/to/foo.txt')) { // true / false
    throw new Exception ("The file does not exists");
}

Seeks

Seeks on a file pointer.
 
if ($fd = fopen("file.txt", "w+")) {
    fwrite($fd, "abcrn");
    fwrite($fd, "def");
    fclose($fd);
}

if ($fd = fopen("file.txt", "r")) {

    fseek($fd, 2); // <span class='keyword_code'>advance 2 chars</span>
    echo fgets($fd) . "<br>"; // c

    fseek($fd, 2, SEEK_SET); // default (go to begging of the file)
    echo fgets($fd) . "<br>"; // c

    fseek($fd, 0, SEEK_CUR); // start from current position
    echo fgets($fd) . "<br>"; // def

    fseek($fd, -2, SEEK_END); // start from the end of the file
    echo fgets($fd) . "<br>"; // ef
}

Tell

Returns the current position of the file read/write pointer (or FALSE).
 
$fd = fopen("example.txt", "r");
fseek($fd, 2);
echo <span class='keyword_code'>ftell($fd)</span>; // 2
 
$fd = fopen("example.txt", "r");
$data = fgets($fd, 2);
echo ftell($fd); // 1



  Last update: 233 days ago