Trouble with file()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Paul Oertel
Forum Newbie
Posts: 18
Joined: Fri May 31, 2002 3:44 am
Location: Japan

Trouble with file()

Post by Paul Oertel »

I am trying to read a group of text files that contain parts of a report. My code looks like this:

Code: Select all

$fp = fopen("m:\\html_parts\\html02.txt", "r");
$filearray = file($fp);
The following error is returned:

PHP Warning: file() expects parameter 1 to be string, resource given in m:\reportnt_test.php on line 46

I would appreciate help getting over this bump in the road.

Paul
rd64pro
Forum Newbie
Posts: 7
Joined: Tue Jun 18, 2002 6:10 pm
Location: Sacramento, CA

Post by rd64pro »

The problem is that both 'fopen' and 'file' open files, with the path to the file specified as a string. I'm not exactly clear on what specifically you wanted out of those two statements, so I'll just give a couple examples:

// this will open the file and return an array of all lines in the file
$contents = file("m:\\html_parts\\html02.txt");

// this, however, only returns a handle to the file specified. After you
// have this handle, you can perform read operations using functions
// such as 'fread', etc.
$fp = fopen("m:\\html_parts\\html02.txt", 'r');

Since you are dealing with straight text here, my guess is you DO want an array of lines from the file, so go with example 1.

I hope this helps!

-Ryan
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You don't need to do fopen() in order to use file(), have you tried

Code: Select all

$filearray = file('m:\\html_parts\\html02.txt');
Mac
Paul Oertel
Forum Newbie
Posts: 18
Joined: Fri May 31, 2002 3:44 am
Location: Japan

Post by Paul Oertel »

You're right. Thanks for the help everybody.

Paul
Post Reply