Tools, FAQ, Tutorials:
Opening a file for Reading in PHP
How To Open a File for Reading in PHP?
✍: FYIcenter.com
If you want to open a file and read its contents piece by piece, you can use the fopen($fileName, "r") function. It opens the specified file, and returns a file handle. The second argument "r" tells PHP to open the file for reading. Once the file is open, you can use other functions to read data from the file through this file handle. Here is a PHP script example on how to use fopen() for reading:
<?php
$file = fopen("/windows/system32/drivers/etc/hosts", "r");
print("Type of file handle: " . gettype($file) . "\n");
print("The first line from the file handle: " . fgets($file));
fclose($file);
?>
This script will print:
Type of file handle: resource The first line from the file handle: # Copyright (c) 1993-1999
Note that you should always call fclose() to close the opened file when you are done with the file.
⇒ Opening a File for Writing in PHP
⇐ Reading the Entire File to a String in PHP
2016-12-02, ∼2686🔥, 0💬
Popular Posts:
How To Open Standard Output as a File Handle in PHP? If you want to open the standard output as a fi...
How to use "link" command tool to link objet files? If you have object files previously compiled by ...
Why I am getting "LNK1104: cannot open file 'MSCOREE.lib'" error when building a C++/CLI program? Vi...
What Happens If One Row Has Missing Columns? What happens if one row has missing columns? Most brows...
How To Avoid the Undefined Index Error in PHP? If you don't want your PHP page to give out errors as...