-2

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 ?

user1561466
  • 71
  • 1
  • 3
  • 11

4 Answers4

4

This is a simpler way:

$theData = file_get_contents("/path/to/file/" . $myFile);
Maerlyn
  • 32,079
  • 17
  • 92
  • 82
3

First off, it'd be simper just to use file_get_contents()

As to the file path, that needs to be a string, i.e.:

$fh = fopen("/path/to/".$myFile, 'r');
SomeKittens
  • 35,809
  • 19
  • 104
  • 135
1
$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...

silentw
  • 4,479
  • 4
  • 22
  • 44
0

I have tried to solve this problem in my own way. Hope this will help you.

<?php

$myFile = "folder/file.txt";
$myHandle = fopen($myFile, "r");

$myData = fread($myHandle, filesize($myFile));
$rows = explode(" ", $myData);

foreach($rows as $row) 
{
    print $row;
}

fclose($myHandle);

?> 
Bart
  • 18,467
  • 7
  • 66
  • 73
Badar
  • 1,312
  • 13
  • 18