Opening File And Loading Contents Into A Variable

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2134 Views - Last Post: 05 September 2002 - 05:59 PM Rate Topic: -----

#1 fyrestorm   User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Opening File And Loading Contents Into A Variable

Posted 31 August 2002 - 11:55 AM

is it possible to open file and load it's contents into a variable?
Is This A Good Question/Topic? 0
  • +

Replies To: Opening File And Loading Contents Into A Variable

#2 Spider   User is offline

  • Arachnid

Reputation: 2
  • View blog
  • Posts: 769
  • Joined: 10-July 02

Re: Opening File And Loading Contents Into A Variable

Posted 31 August 2002 - 12:08 PM

to do that you have two options, depending on what version of php your running.

if your server is running PHP 4 CVS only then you can use the file_get_contents function.

if not, or if your unsure, you can use this script:

$filename = "file.html";
$fd = fopen ($filename, "r");
$contents = fread ($fd, filesize ($filename));
fclose ($fd);


this will open file.html and put the contents into the variable $contents
Was This Post Helpful? 0
  • +
  • -

#3 fyrestorm   User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Opening File And Loading Contents Into A Variable

Posted 31 August 2002 - 12:10 PM

Spider, on Aug 31 2002, 01:08 PM, said:

to do that you have two options, depending on what version of php your running.

if your server is running PHP 4 CVS only then you can use the file_get_contents function.

if not, or if your unsure, you can use this script:

$filename = "file.html";
$fd = fopen ($filename, "r");
$contents = fread ($fd, filesize ($filename));
fclose ($fd);


this will open file.html and put the contents into the variable $contents

coolness thanks :)
Was This Post Helpful? 0
  • +
  • -

#4 fyrestorm   User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Opening File And Loading Contents Into A Variable

Posted 31 August 2002 - 12:16 PM

not that i doubted it or anything, but awsome, it works :)
Was This Post Helpful? 0
  • +
  • -

#5 Spider   User is offline

  • Arachnid

Reputation: 2
  • View blog
  • Posts: 769
  • Joined: 10-July 02

Re: Opening File And Loading Contents Into A Variable

Posted 31 August 2002 - 12:31 PM

*sigh*, I answer all these questions for people and I don't get contribution points for it.

and yet I wouldn't have a clue what to contribute to the tutorials and articles sections...

ah well, I guess I'm doomed to answering questions without reward ;)
Was This Post Helpful? 0
  • +
  • -

#6 skyhawk133   User is offline

  • Head DIC Head
  • member icon

Reputation: 1972
  • View blog
  • Posts: 20,425
  • Joined: 17-March 01

Re: Opening File And Loading Contents Into A Variable

Posted 31 August 2002 - 12:33 PM

Since you said something and you do help out a lot, I'll give you 50 contribution points for the help you've given lately. :)

Thanks! Keep up the great work!
Was This Post Helpful? 0
  • +
  • -

#7 Cookie Mobster   User is offline

  • nooneenooneenooonee
  • member icon

Reputation: 8
  • View blog
  • Posts: 4,736
  • Joined: 12-October 01

Re: Opening File And Loading Contents Into A Variable

Posted 31 August 2002 - 12:35 PM

be very weary about using these types of tricks though... I would always specify a maximum read size so you don't overload your webservers memory. :)
Was This Post Helpful? 0
  • +
  • -

#8 fyrestorm   User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Opening File And Loading Contents Into A Variable

Posted 31 August 2002 - 03:57 PM

smckone, on Aug 31 2002, 01:35 PM, said:

be very weary about using these types of tricks though... I would always specify a maximum read size so you don't overload your webservers memory. :)

do you know of another more safer way to do this?
Was This Post Helpful? 0
  • +
  • -

#9 Spider   User is offline

  • Arachnid

Reputation: 2
  • View blog
  • Posts: 769
  • Joined: 10-July 02

Re: Opening File And Loading Contents Into A Variable

Posted 31 August 2002 - 04:42 PM

if you want to specify a maximum filesize, you just replace the $filesize with a numerical value.
$contents = fread ($fd, filesize ($filename));


so for a max of 1000 it would be:
$contents = fread ($fd, filesize (1000));


beyond that there isn't a lot you can do.

although unless you open a truly huge file your not going to actually overload your webservers memory :)

btw, depending on what your actually opening the file for, 1000 may be quite a lot too small
Was This Post Helpful? 0
  • +
  • -

#10 iordy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 05-March 02

Re: Opening File And Loading Contents Into A Variable

Posted 02 September 2002 - 09:52 PM

To save a few strokes on my golf score I sometimes use:
$file = implode('',file($filename));


Was This Post Helpful? 0
  • +
  • -

#11 fyrestorm   User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Opening File And Loading Contents Into A Variable

Posted 03 September 2002 - 07:21 AM

so what's the difference between implode and fread?
Was This Post Helpful? 0
  • +
  • -

#12 iordy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 05-March 02

Re: Opening File And Loading Contents Into A Variable

Posted 03 September 2002 - 11:22 PM

You dont need fopen() etc. The line you see there does the same thing as:
$fd = fopen ($filename, "r");
$contents = fread ($fd, filesize ($filename));
fclose ($fd);



except it does it all in one line. It's just a different and shorter/quicker way to load a file into a variable.
Was This Post Helpful? 0
  • +
  • -

#13 fyrestorm   User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Opening File And Loading Contents Into A Variable

Posted 04 September 2002 - 07:20 AM

ok, so basically, i can use this one line of code:

$file = implode('',file($filename));



instead of these three lines?

$fd = fopen ($filename, "r");
$contents = fread ($fd, filesize ($filename));
fclose ($fd);



sweet :)
Was This Post Helpful? 0
  • +
  • -

#14 iordy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 05-March 02

Re: Opening File And Loading Contents Into A Variable

Posted 04 September 2002 - 08:23 PM

yep.

To explain further, file() reads a file into an array with each line of the file in a seperate array element. then implode takes the the array returned by file and 'implodes' the array back into a single string which is returned to the variable $file.
Was This Post Helpful? 0
  • +
  • -

#15 Spider   User is offline

  • Arachnid

Reputation: 2
  • View blog
  • Posts: 769
  • Joined: 10-July 02

Re: Opening File And Loading Contents Into A Variable

Posted 05 September 2002 - 08:55 AM

whilst I admit that this is a nice way of saving your poor keyboard from extra typing, it removes the option of limiting the size of the file...

this shouldn't be a big problem so long as your sure of the files your opening, but the longer option can be made safer, as stated above :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2