PHP

6 WAYS TO READ FILES IN PHP

(quick guide & examples)

READ FROM FILE $text = file_get_contents('FILE');

READ FILE TO STRING

01

OR READ FROM URL $text = file_get_contents('URL');

EACH LINE IN THE FILE WILL BE AN ELEMENT IN THE ARRAY $array = file('FILE');

READ FILE TO ARRAY

02

CURL INIT $curl = curl_init("URL"); curl_setopt($curl,  CURLOPT_RETURNTRANSFER, 1);

CURL FETCH

03

CURL FETCH $data = curl_exec($curl);

CURL CLOSE curl_close($curl); echo $data;

OPEN FILE STREAM $handle = fopen("FILE", "r");

READ LINE-BY-LINE

04

READ LINE-BY-LINE while (($line = fgets($handle)) !== false) { echo $line; }

CLOSE FILE STREAM fclose($handle);

OUTPUT HTTP HEADERS header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"FILE\""); header("Pragma: public"); header("Content-Length: ".filesize("FILE"));

FILE STREAM

05

READ & OUTPUT STREAM readfile('FILE');

<!DOCTYPE HTML> <html> <body>   <?php require "FILE.HTML"; ?> </body> </html>

INCLUDE & REQUIRE

06