Jump to content


Problems with including a text file

  • Please log in to reply
1 reply to this topic

#1 Hartley

  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 06 July 2006 - 08:18 AM

I'm struggling with some code. There are 2 separate scripts I wish to make. First, I have a file, which for the sake of this topic am going to refer to as filetoinclude.txt. I need a script with a basic form that has a single line in a form and a submit button that will change the text in this file.

The second part I already have a script for, but I want to define a variable as the contents of that text file. So assuming filetoinclude.txt has only the following "Testing Text", I just want to define variable $var as whatever the text is in the text file. Basically this is what I have for defining:

$MOTD = @include("path/to/filetoinclude.txt");

My php is very shaky and these two pieces are giving me some problems. Thanks in advance for any assistance!

Edit: I suppose I asked this question inappropriately: I googled for the functions in both cases without any luck. If no one wishes to answer the first one because it's technically writing a script, that's perfectly understandable. However, if it's possible to find out how to properly define a variable as the contents of a text file, that would be spectacular.

#2 ToonMariner

  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 06 July 2006 - 08:40 AM

you will have to read the contents of the file with file_get_contents in order to use the contents as a variable (if you have older versions of php then you will need fopen, fread and fclose - read the manual on file_get_contents).

imply including a file means that file is trated as part of the script (note php automatically breaks out so if it were a snippett of code you would need to start with <?php in the include file!).
follow me on twitter @PHPsycho

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users