MemoryRefresh!

Read file



Questions 1 Answers 0%

Pages   0 from 51
Questions   0 from 84

Reset



fread() - read arbitrary data from a file
<?php // 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)); // --- Look Here --- // echo $contents . "<br>"; // Output: aaa bbb rewind($fd); echo fread($fd, 2) . "<br>"; // Output: aa rewind($fd); echo fread($fd); // Warning: fread() expects exactly 2 parameters, 1 given } file_exists() - Checks whether a file or directory exists (returns TRUE/FALSE)
<?php if (!file_exists('paht/to/foo.txt')) { throw new Exception ("The file does not exists"); } fseek() - Seeks on a file pointer
<?php if ($fd = fopen("file.txt", "w+")) { fwrite($fd, "abcrn"); fwrite($fd, "def"); fclose($fd); } if ($fd = fopen("file.txt", "r")) { fseek($fd, 2); // advance 2 chars // --- Look Here --- // echo fgets($fd) . "<br>"; // Outputs: c fseek($fd, 2, SEEK_SET); // default (go to begging of the file) echo fgets($fd) . "<br>"; // Outputs: c fseek($fd, 0, SEEK_CUR); // start from current position echo fgets($fd) . "<br>"; // Outputs: def fseek($fd, -2, SEEK_END); // start from the end of the file echo fgets($fd) . "<br>"; // Outputs: ef } ftell() - Returns the current position of the file read/write pointer (or FALSE)
<?php $fd = fopen("example.txt", "r"); fseek($fd, 2); echo ftell($fd); // Output: 2
<?php $fd = fopen("example.txt", "r"); $data = fgets($fd, 2); echo ftell($fd); // Output: 1
Questions

Related pages

File open !
Read file !
Read csv !
File contents !
File cache and stats !
File lock !
File parse !
Context !
Network !


0% 100%
 
 
0 pages 51 pages
1) Read file <?php if ($fd = fopen($file, "r")) { $contents = ____________; }





2) Move the pointer at the start of the file <?php $contents = fread($fd, filesize($file)); // pointer at the end





Last Review: n/a   Passed: n/a   Reviews: n/a   Next Review: n/a  


fread() - read arbitrary data from a file
<?php // 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)); // --- Look Here --- // echo $contents . "<br>"; // Output: aaa bbb rewind($fd); echo fread($fd, 2) . "<br>"; // Output: aa rewind($fd); echo fread($fd); // Warning: fread() expects exactly 2 parameters, 1 given } file_exists() - Checks whether a file or directory exists (returns TRUE/FALSE)
<?php if (!file_exists('paht/to/foo.txt')) { throw new Exception ("The file does not exists"); } fseek() - Seeks on a file pointer
<?php if ($fd = fopen("file.txt", "w+")) { fwrite($fd, "abcrn"); fwrite($fd, "def"); fclose($fd); } if ($fd = fopen("file.txt", "r")) { fseek($fd, 2); // advance 2 chars // --- Look Here --- // echo fgets($fd) . "<br>"; // Outputs: c fseek($fd, 2, SEEK_SET); // default (go to begging of the file) echo fgets($fd) . "<br>"; // Outputs: c fseek($fd, 0, SEEK_CUR); // start from current position echo fgets($fd) . "<br>"; // Outputs: def fseek($fd, -2, SEEK_END); // start from the end of the file echo fgets($fd) . "<br>"; // Outputs: ef } ftell() - Returns the current position of the file read/write pointer (or FALSE)
<?php $fd = fopen("example.txt", "r"); fseek($fd, 2); echo ftell($fd); // Output: 2
<?php $fd = fopen("example.txt", "r"); $data = fgets($fd, 2); echo ftell($fd); // Output: 1



More from Php

Basics
Variables
Operators
Function
Array
String
Streams
Uncategorized


More from Programming

Php
jQuery




Powered by minte9.com