Search code examples
phpfile-read

php file read function


I am using the following file read

$myFile = "file.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;

problem is file.txt is generating dynamically and is in different folder so how do I call it in fopen ? I am trying

$fh = fopen(/path/to/$myFile, 'r');

but obviously its not working and giving me error

PHP Parse error:  syntax error, unexpected '/',

how to rectify this and include my path ?


Solution

  • $myPath = "/path/to/file/";
    $myFile = "file.txt";
    $fh = fopen($myPath.$myFile, 'r');
    $theData = fread($fh, filesize($myFile));
    fclose($fh);
    echo $theData;
    

    I haven't tested it, but I guess it will work that way...