[PHP] best way to read a file

2002-03-28 Thread Erik Price

This is a really simple question that I've wondered for a while now...

what is the best way to read a file?  There are so many different 
functions for reading files that I have no idea which I should use.

readfile() returns its values to standard output -- I don't want to use 
this, since my script needs to load the data in the file into a variable 
(it's a .xsl file).  fgets() looks like it could work if I made an array 
out of each line read and then imploded them.  fread() looks like the 
'main' function for reading from files.  fpassthru() -- I guess if 
you're already in the middle of a file it's a good way to finish?

There are other functions for reading files, but they are less 
ambiguous, with purposes like reading from sockets or certain kinds of 
files.  I'm not interested in those -- I just want to know which 
function I should use to simply read a file and store the file's 
contents in a variable (for later actions to be performed on/with this 
variable).

Thank you.


Erik







Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] best way to read a file

2002-03-28 Thread Kevin Stone

Actually if you have output buffering active then the easiest hassle
free way to read a file into a variable is like this..

?
ob_start();
readfile($url, 1);
$file = ob_get_contents();
ob_end_clean();

echo $file;
?

Pretty cool eh?  Hope that helps.

--
Kevin Stone
[EMAIL PROTECTED]

  
-Original Message-
From: Erik Price [mailto:[EMAIL PROTECTED]] 
Sent: Thursday, March 28, 2002 5:03 PM
To: [EMAIL PROTECTED]
Subject: [PHP] best way to read a file

This is a really simple question that I've wondered for a while now...

what is the best way to read a file?  There are so many different 
functions for reading files that I have no idea which I should use.

readfile() returns its values to standard output -- I don't want to use 
this, since my script needs to load the data in the file into a variable

(it's a .xsl file).  fgets() looks like it could work if I made an array

out of each line read and then imploded them.  fread() looks like the 
'main' function for reading from files.  fpassthru() -- I guess if 
you're already in the middle of a file it's a good way to finish?

There are other functions for reading files, but they are less 
ambiguous, with purposes like reading from sockets or certain kinds of 
files.  I'm not interested in those -- I just want to know which 
function I should use to simply read a file and store the file's 
contents in a variable (for later actions to be performed on/with this 
variable).

Thank you.


Erik







Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] best way to read a file

2002-03-28 Thread bvr

you didn't mention file()

that one can be pratical because it returns an array of lines (linefeed 
intact, implode with empty string),
but can be memory consuming for large files, because the entire file is 
in memory
the at the same time.

some times you don't need the entire file (at a time) ..
for example
fread() allows you to read a specific length
fgets() one text line at a time with max length
fgetss() ..and strip HTML..
fgetcsv() or return a line from a .csv as an array..

not really ambiguous,
and I think these functions being flexible enough to handle other 
resource types can hardly be seen as a disadvantage..

bvr.

Erik Price wrote:

 This is a really simple question that I've wondered for a while now...

 what is the best way to read a file?  There are so many different 
 functions for reading files that I have no idea which I should use.

 readfile() returns its values to standard output -- I don't want to 
 use this, since my script needs to load the data in the file into a 
 variable (it's a .xsl file).  fgets() looks like it could work if I 
 made an array out of each line read and then imploded them.  fread() 
 looks like the 'main' function for reading from files.  fpassthru() -- 
 I guess if you're already in the middle of a file it's a good way to 
 finish?

 There are other functions for reading files, but they are less 
 ambiguous, with purposes like reading from sockets or certain kinds of 
 files.  I'm not interested in those -- I just want to know which 
 function I should use to simply read a file and store the file's 
 contents in a variable (for later actions to be performed on/with this 
 variable).

 Thank you.


 Erik





 

 Erik Price
 Web Developer Temp
 Media Lab, H.H. Brown
 [EMAIL PROTECTED]





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] best way to read a file

2002-03-28 Thread Erik Price


On Thursday, March 28, 2002, at 07:34  PM, bvr wrote:

 not really ambiguous,

(actually, that's what I was saying, that they are -less- ambiguous, and 
more specific, but no big deal on that little matter).

 and I think these functions being flexible enough to handle other 
 resource types can hardly be seen as a disadvantage..

Every time I try to use one of these functions, I get an error message 
from my browser (not from PHP) that the attempt to load the URL failed.  
Is this a sign of running into memory problems?  If so, what can I do 
about it?  It seems that the only file-reading function that doesn't 
create this error message in my script is fgets(), but as soon as I try 
to implode the lines I pulled with fgets() into one string, I get the 
error message again.  In other words, every time I try to pull my XSL 
file into my script, it happens.

(I would not have imagined I would run out of memory b/c I have php.ini 
set to 16M per script and the XSL file is only 8kB, though the script 
does do a lot of things.)


Anybody know what's going on here?


Erik





Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] best way to read a file

2002-03-28 Thread bvr


 not really ambiguous,

 (actually, that's what I was saying, that they are -less- ambiguous, 
 and more specific, but no big deal on that little matter). 

ok, misunderstood


 Every time I try to use one of these functions, I get an error message 
 from my browser (not from PHP) that the attempt to load the URL 
 failed.  Is this a sign of running into memory problems?  If so, what 
 can I do about it?  It seems that the only file-reading function that 
 doesn't create this error message in my script is fgets(), but as soon 
 as I try to implode the lines I pulled with fgets() into one string, I 
 get the error message again.  In other words, every time I try to pull 
 my XSL file into my script, it happens.

 (I would not have imagined I would run out of memory b/c I have 
 php.ini set to 16M per script and the XSL file is only 8kB, though the 
 script does do a lot of things.)


 Anybody know what's going on here? 


Try not to use MSIE when developing or install the debug thingie, 
otherwise you will not get sensible feedback on what went wrong during 
page load..

Anyway, it's probably not the memory limit, because in that case PHP 
reports an error..

Did you even enable-memory-limit ?? If you did, use can try this with 
your httpd.conf :

Add somewhere global :
LogFormat %h %l %u %t \%r\ %s %b %{mod_php_memory_usage}n php

Add to mainserver/virtualhost:
CustomLog /usr/local/apache/logs/access_log php

This adds an extra value to the access log entries that indicates the 
largest number of bytes of memory used by the script at a time.

bvr.







-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] best way to read a file

2002-03-28 Thread Justin French

while we're on the topic, what's the best way to append an CSV line onto the
end of a text file?

---mail.csv---
Justin,French,[EMAIL PROTECTED]\n
Fred,Flintstone,[EMAIL PROTECTED]\n
Barny,Rubble,[EMAIL PROTECTED]\n
---

I'd like to append a new line::

$newline = Hank,Foo,[EMAIL PROTECTED]\n;

...onto the end of file.  I guess the process is, but I'm keen to hear
improvements :)


?

$newline = Hank,Foo,[EMAIL PROTECTED]\n;
$file = data/mail.csv;

$fp = fopen($file,'a+');
flock ($fp,2);
fwrite($fp,$newline);
fclose($fp);

?

I can't find fulock(), so I guess closing the $fp unlocks the lock.

Many thanks,

Justin French

Creative Director
http://Indent.com.au



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] best way to read a file

2002-03-28 Thread Erik Price


On Thursday, March 28, 2002, at 08:14  PM, bvr wrote:

 Try not to use MSIE when developing or install the debug thingie, 
 otherwise you will not get sensible feedback on what went wrong during 
 page load..

Good advice, I'll try with Mozilla.

 Anyway, it's probably not the memory limit, because in that case PHP 
 reports an error..

I'm starting to suspect the same, read on...

 Did you even enable-memory-limit ?? If you did, use can try this with 
 your httpd.conf

No, I didn't... I just noticed that in php.ini there is a setting for 
max memory per script (default was 8M, I raised it to 16 before, when I 
thought this was the problem).

I believe the problem is the xslt_process() function.  I actually was 
able to read the file in my script after all, so I'm almost positive 
that it's not a problem with that.  No, what happened in fact was that 
if the file was read successfully, it was passed as an argument to the 
xslt_process() function.  If xslt_process() was the source of my 
problem, then that would explain why it crapped out every time I 
included file-reading functions -- because I wasn't testing in 
isolation, rather, I was testing with the xslt_process() function and 
couldn't see the forest for the trees.

I'm going to try some of the other methods for using xslt_process() 
listed at the man page, I was trying the simpler 3-argument version.  
There's a more complex 6-argument function that I will try now, and see 
if that works.  But it seems that I only get this error when attempting 
a successful xslt_process().  (I know that it is successful because I am 
testing its success in the script.)

Thanks, bvr.  I'll continue to bug test.



Erik






Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php