PHP

6 WAYS TO READ FILES IN PHP

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

READ FILE TO STRING

01

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 while (($line = fgets($handle)) !== false) { echo $line; }   CLOSE FILE STREAM fclose($handle);

READ LINE-BY-LINE

04

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

FILE STREAM

05

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

INCLUDE & REQUIRE

06