MemoryRefresh!

File open



Questions 1 Answers 0%

Pages   0 from 51
Questions   0 from 84

Reset



There are two types of streams (files & network). - php.* - standard PHP input/output - file - standard file access - http - access to remote resources via HTTP - ftp - access to remote resources via FTP - compress.zlib - access to compressed data stream using the zlib library fopen() - open a file
<?php $filename = "counter.txt"; $counter = 0; if ($fd = @fopen($filename, "r")) { // --- Look Here --- // $counter = (int) fgets($fd); // gets first line content fclose($fd); } $fw = fopen($filename, "w+"); // open for reading and writing // --- Look Here --- // fwrite($fw, ++$counter); // writes new counter value fclose($fw); echo "There has been $counter hits to this page"; /* r - reading only (pointer at the beginning of the file) r+ - reading and writing (pointer at the beginning) w - writing only w+ - writing and reading a - writing only (pointer at the end) a+ - reading and writing (pointer at the end) x - creates a new file for writing only x+ - creates a new file for writing and reading w+b - forces binary mode (unaltered date will be written) w+t - windows only (Unix n will be converted to Windows rn) w, w+, a, a+ - automaticaly create a new file if not exists x, x+ - throw a worning if the file exists */ fgets() - gets line from file pointer
<?php $file = "file.txt"; if ($fd = fopen($file, "w+")) { fwrite($fd, "aaarn"); fwrite($fd, "bbb"); fclose($fd); } if ($fd = fopen($file, "r")) { $aLine = fgets($fd); // 1024 byte length (default) // --- Look Here --- // echo $aLine . "<br>"; // Outputs: aaa rewind($fd); // --- Look Here --- // $aByte = fgets($fd, 1); // 1 byte length echo "^{$aByte}$" . "<br>"; // Outputs: ^$ (new line character) rewind($fd); $aByte2 = fgets($fd, 2); // 2 bytes echo "^{$aByte2}$" . "<br>"; // Outputs: a $ rewind($fd); $aByte5 = fgets($fd, 5); // 5 bytes echo "^{$aByte5}$" . "<br>"; // Outputs: aaar $ } feof() - determines when the internal pointer hits the end of a file
<?php $file = "file.txt"; if ($fd = fopen($file, "w+")) { fwrite($fd, "aaarn"); fwrite($fd, "bbb"); fclose($fd); } if ($fd = fopen($file, "r")) { $buffer = ""; while(!feof($fd)) { // --- Look Here --- // $buffer .= fgets($fd); } echo $buffer; // Outputs: aaa bbb }
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) Open a file for writing. <?php if ($fd = ______________) { fwrite($fd, "aaa"); }





2) How do you read a line from a file? <?php $fd = fopen("example.txt", "r"); echo _______________;




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


There are two types of streams (files & network). - php.* - standard PHP input/output - file - standard file access - http - access to remote resources via HTTP - ftp - access to remote resources via FTP - compress.zlib - access to compressed data stream using the zlib library fopen() - open a file
<?php $filename = "counter.txt"; $counter = 0; if ($fd = @fopen($filename, "r")) { // --- Look Here --- // $counter = (int) fgets($fd); // gets first line content fclose($fd); } $fw = fopen($filename, "w+"); // open for reading and writing // --- Look Here --- // fwrite($fw, ++$counter); // writes new counter value fclose($fw); echo "There has been $counter hits to this page"; /* r - reading only (pointer at the beginning of the file) r+ - reading and writing (pointer at the beginning) w - writing only w+ - writing and reading a - writing only (pointer at the end) a+ - reading and writing (pointer at the end) x - creates a new file for writing only x+ - creates a new file for writing and reading w+b - forces binary mode (unaltered date will be written) w+t - windows only (Unix n will be converted to Windows rn) w, w+, a, a+ - automaticaly create a new file if not exists x, x+ - throw a worning if the file exists */ fgets() - gets line from file pointer
<?php $file = "file.txt"; if ($fd = fopen($file, "w+")) { fwrite($fd, "aaarn"); fwrite($fd, "bbb"); fclose($fd); } if ($fd = fopen($file, "r")) { $aLine = fgets($fd); // 1024 byte length (default) // --- Look Here --- // echo $aLine . "<br>"; // Outputs: aaa rewind($fd); // --- Look Here --- // $aByte = fgets($fd, 1); // 1 byte length echo "^{$aByte}$" . "<br>"; // Outputs: ^$ (new line character) rewind($fd); $aByte2 = fgets($fd, 2); // 2 bytes echo "^{$aByte2}$" . "<br>"; // Outputs: a $ rewind($fd); $aByte5 = fgets($fd, 5); // 5 bytes echo "^{$aByte5}$" . "<br>"; // Outputs: aaar $ } feof() - determines when the internal pointer hits the end of a file
<?php $file = "file.txt"; if ($fd = fopen($file, "w+")) { fwrite($fd, "aaarn"); fwrite($fd, "bbb"); fclose($fd); } if ($fd = fopen($file, "r")) { $buffer = ""; while(!feof($fd)) { // --- Look Here --- // $buffer .= fgets($fd); } echo $buffer; // Outputs: aaa bbb }



More from Php

Basics
Variables
Operators
Function
Array
String
Streams
Uncategorized


More from Programming

Php
jQuery




Powered by minte9.com