RE: [PHP] Opening a file to manipulate it

2002-11-20 Thread Troy May
Thanks Ed!  Yes, another member here told me to use fread(), which is what
your link led to if you're not using the CVS version of PHP.  (whatever that
is)

I got it working with this:

?
$file = ../header.html;
$fp = fopen($file, r);
$header = fread($fp, filesize($file));
fclose ($fp);

echo $header;

I'm a Perl guy so I was looking for an easier way.  In Perl, all you have to
do is open it, dump it to a variable and close it.  I thought it would be
that easy in PHP too.  I guess Perl wins there, but PHP is much better
overall so far.  ;)

Thanks again!

Troy

-Original Message-
From: @ Edwin [mailto:[EMAIL PROTECTED]]
Sent: Saturday, November 16, 2002 9:29 PM
To: Troy May
Cc: [EMAIL PROTECTED]
Subject: Re: [PHP] Opening a file to manipulate it


Hello,

Troy May [EMAIL PROTECTED] wrote:
 Hello,

 Extreme newbie here (halfway through my first book :) ).  I need to open a
 file, read the whole file into a variable, manipulate it, and then echo it
 to the screen.  I'm doing it right from my book and it's not working.
 Here's what I have now:

 ? $header = fopen(header.html,r);

 echo $header; ?

 It writes Resource id #1 at the top of the page.  What is that?!  :)

Check the manual:

  http://www.php.net/manual/en/function.fopen.php

You're echoing the result of fopen() which is an int--you have to make
sure that you're using the function properly ;)

 I have also tried using file().  That displays Array on the screen.
What
 am I doing wrong?

I think it's because you're echoing the result of file() which is an array.
You cannot just echo $the_array; Try print_r($the_array) and you'll see a
different result--though I doubt it's the one you want to see :)

Check the manual again:

  http://www.php.net/manual/en/function.file.php

Anyway, I think you're looking for something like this:

  http://www.php.net/manual/en/function.file-get-contents.php

Esp. the User Contributed Notes.

HTH,

- E


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




RE: [PHP] Opening a file to manipulate it

2002-11-20 Thread Tarsy
Try this:

$content = implode('', file('../header.html'));

Or if you're running php 4.3.0 or greater:

$content = file_get_contents('../header.html');

Can't get much easier than that :)

-Mark
PS: That was in the user contributed notes Ed mentioned :)

- Original message -
From: Troy May [EMAIL PROTECTED]
To: @ Edwin [EMAIL PROTECTED]
Date: Sat, 16 Nov 2002 21:45:50 -0800
Subject: RE: [PHP] Opening a file to manipulate it

Thanks Ed!  Yes, another member here told me to use fread(), which is
what
your link led to if you're not using the CVS version of PHP.  (whatever
that
is)

I got it working with this:

?
$file = ../header.html;
$fp = fopen($file, r);
$header = fread($fp, filesize($file));
fclose ($fp);

echo $header;

I'm a Perl guy so I was looking for an easier way.  In Perl, all you have
to
do is open it, dump it to a variable and close it.  I thought it would be
that easy in PHP too.  I guess Perl wins there, but PHP is much better
overall so far.  ;)

Thanks again!

Troy

-Original Message-
From: @ Edwin [mailto:[EMAIL PROTECTED]]
Sent: Saturday, November 16, 2002 9:29 PM
To: Troy May
Cc: [EMAIL PROTECTED]
Subject: Re: [PHP] Opening a file to manipulate it


Hello,

Troy May [EMAIL PROTECTED] wrote:
 Hello,

 Extreme newbie here (halfway through my first book :) ).  I need to open a
 file, read the whole file into a variable, manipulate it, and then echo it
 to the screen.  I'm doing it right from my book and it's not working.
 Here's what I have now:

 ? $header = fopen(header.html,r);

 echo $header; ?

 It writes Resource id #1 at the top of the page.  What is that?!  :)

Check the manual:

  http://www.php.net/manual/en/function.fopen.php

You're echoing the result of fopen() which is an int--you have to make
sure that you're using the function properly ;)

 I have also tried using file().  That displays Array on the screen.
What
 am I doing wrong?

I think it's because you're echoing the result of file() which is an
array.
You cannot just echo $the_array; Try print_r($the_array) and you'll see
a
different result--though I doubt it's the one you want to see :)

Check the manual again:

  http://www.php.net/manual/en/function.file.php

Anyway, I think you're looking for something like this:

  http://www.php.net/manual/en/function.file-get-contents.php

Esp. the User Contributed Notes.

HTH,

- E


-- 
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] Opening a file to manipulate it

2002-11-16 Thread @ Edwin
Hello,

Troy May [EMAIL PROTECTED] wrote:
 Hello,

 Extreme newbie here (halfway through my first book :) ).  I need to open a
 file, read the whole file into a variable, manipulate it, and then echo it
 to the screen.  I'm doing it right from my book and it's not working.
 Here's what I have now:

 ? $header = fopen(header.html,r);

 echo $header; ?

 It writes Resource id #1 at the top of the page.  What is that?!  :)

Check the manual:

  http://www.php.net/manual/en/function.fopen.php

You're echoing the result of fopen() which is an int--you have to make
sure that you're using the function properly ;)

 I have also tried using file().  That displays Array on the screen.
What
 am I doing wrong?

I think it's because you're echoing the result of file() which is an array.
You cannot just echo $the_array; Try print_r($the_array) and you'll see a
different result--though I doubt it's the one you want to see :)

Check the manual again:

  http://www.php.net/manual/en/function.file.php

Anyway, I think you're looking for something like this:

  http://www.php.net/manual/en/function.file-get-contents.php

Esp. the User Contributed Notes.

HTH,

- E

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




Re: [PHP] Opening a file to manipulate it

2002-11-16 Thread Justin French
Hi,

on 17/11/02 1:45 PM, Troy May ([EMAIL PROTECTED]) wrote:

 ? $header = fopen(header.html,r);
 
 echo $header; ?
 
 It writes Resource id #1 at the top of the page.  What is that?!  :)


You're missing some code -- instead, try this example from the fread() page
in the manual:

?php
// get contents of a file into a string
$filename = header.html;
$fd = fopen ($filename, r);
$contents = fread ($fd, filesize ($filename));
fclose ($fd);
?

Cheers,


Justin French

http://Indent.com.au
Web Developent  
Graphic Design



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