Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/50fe4d8b18ce6549e90cbc765b541bf9 to your computer and use it in GitHub Desktop.
Save code-boxx/50fe4d8b18ce6549e90cbc765b541bf9 to your computer and use it in GitHub Desktop.
PHP Read Files

WAYS TO READ FILES IN PHP

https://code-boxx.com/php-read-file/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?php
// (A) READ ENTIRE CONTENTS INTO A STRING
$text = file_get_contents("README.txt");
echo $text;
// (B) ALSO CAN FETCH FROM URL
$text = file_get_contents("https://en.wikipedia.org/wiki/Aha_ha");
echo $text;
<?php
// (A) FILE TO ARRAY
$array = file("README.txt");
print_r($array);
// (B) ADDITIONAL OPTION - SKIP EMPTY LINES
$array = file("README.txt", FILE_SKIP_EMPTY_LINES);
print_r($array);
<?php
$curl = curl_init("https://en.wikipedia.org/wiki/Aha_ha");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
echo $data;
<?php
// (A) OPEN FILE
$handle = fopen("README.txt", "r") or exit("Error reading file!");
// (B) READ LINE BY LINE
while ($line = fgets($handle)) {
// you can also specify how many bytes to read at once
// while (($line = fgets($handle, 4096)) !== false) {
echo $line;
}
// (C) CLOSE FILE
fclose($handle);
<?php
// (A) START OUTPUT BUFFER
$file = "README.txt";
ob_start();
// (B) HTTP HEADERS TO FORCE DOWNLOAD
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$file\"");
header("Expires: 0");
header("Cache-Control: must-revalidate");
header("Pragma: public");
header("Content-Length: ".filesize($file));
// (C) OUTPUT ALL THE HEADERS & STOP BUFFERING
ob_end_flush();
// (D) READ AND OUTPUT FILE DIRECTLY
readfile($file);
exit();
<!DOCTYPE HTML>
<html>
<body>
<?php require "README.txt"; ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment