Jump to content


Grabbing a php file as a variable.


9 replies to this topic

#1 influct

    Young Padawan

  • Members
  • Pip
  • 63 posts

Posted 07 December 2006 - 07:09 AM

I'm in a bit of a pickle: I have a config.php file that looks something like:
<?php
//One set of variables
$variable1 = "omfg";
$variable2 = "hoha";
$variable3 = "rofl";

//second lot of variables
$variable1b = "omfg";
$variable2b = "hoha";
$variable3b = "rofl";

//third lot of variables
$variable1c = "omfg";
$variable2c = "hoha";
$variable3c = "rofl";
?>

You get the idea


The problem is I've no idea how to get config.php translated into a variable,

Normally something like:
<?php
$myFile = "config.php";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
echo" $theData ";
?>

This comes up blank, I need $theData to echo the code of config.php

Any suggestions:S
Thanks:D

#2 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 07 December 2006 - 07:48 AM

if you want the contents of the file stored into an array you use:

$myfile = file_get_contents('config.php');

Matt

#3 influct

    Young Padawan

  • Members
  • Pip
  • 63 posts

Posted 07 December 2006 - 08:01 AM

can i get that in context please?:S

$myfile = file_get_contents('config.php');
echo"$myfile";

just turns up blank:(

Sorry about this, I'm not familiar with flat files.
Thank you!

#4 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 07 December 2006 - 10:46 AM

well how about you tell us why you want to get the file into a variable and what you are going to do with it so we can help you better.

Matt

#5 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 07 December 2006 - 11:54 AM

If I understand what the final result you want is....


Why not just do...
include "config.php";

then you've got all the variables available in your main application in you can do whatever you want with them.

#6 influct

    Young Padawan

  • Members
  • Pip
  • 63 posts

Posted 07 December 2006 - 12:13 PM

basically the idea is that a user can update their settings in config.php
I want to do this by doing something like this:
grab config.php as text, not as php
split it up (I know how to do this bit:D)
Insert the new settings where the old were, then put back together and write to file.
Becuase of the way im doing this I need to handle the php like its text:D
Thanks very much:)

#7 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 07 December 2006 - 01:13 PM

View Postinfluct, on Dec 7 2006, 11:13 AM, said:

basically the idea is that a user can update their settings in config.php
I want to do this by doing something like this:
grab config.php as text, not as php
split it up (I know how to do this bit:D)
Insert the new settings where the old were, then put back together and write to file.
Becuase of the way im doing this I need to handle the php like its text:D
Thanks very much:)


ahh, then you don't want to wrap it with the <?php ?> or even assign it variables.

Just make a basic text flat file with the information.

omfg
rofl
haha

then use something like this:
<?php
$myFile = "config.php";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
echo" $theData ";
?>

to read it into an array variable.

#8 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 07 December 2006 - 05:31 PM

So here's my problem. He says he's trying to echo out the following:
<?php
// List of variables...
?>
But the problem is "it" is turning up blank. What "it" is, i don't know, but i'm willing to assume it's his screen. If thats the case, have you tried viewing your source to see if it was there? Since you're dealing with HTML, anything inbetween a < and a > won't show up unless you use htmlspecialchars().

So try this:
$data = file_get_contents('config.php');
// Or if you're php version is < 4.1:
// $data = implode('', file('config.php'));
echo '<pre>'.htmlspecialchars($data).'</pre>';
That should get something to display on your screen (provided it was showing up in the source to begin with, and you just never checked that).

Spatial, you gotta remember. It's a config file, so he'll still need the opening/closing php tags, and the variable assignments. I'm sure he's simply including the file to load the variables for the specific users, and what we're doing here is attempting to modify the file.

An easy way to do all of this would be to simply use an ini file along with parse_ini_file().

#9 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 08 December 2006 - 01:51 AM

View Postrc69, on Dec 7 2006, 04:31 PM, said:

Spatial, you gotta remember. It's a config file, so he'll still need the opening/closing php tags, and the variable assignments. I'm sure he's simply including the file to load the variables for the specific users, and what we're doing here is attempting to modify the file.

An easy way to do all of this would be to simply use an ini file along with parse_ini_file().

I figured the way I posted, each user having their own flat file, seemed easier to comprehend but I like the INI idea A LOT better. :P

#10 influct

    Young Padawan

  • Members
  • Pip
  • 63 posts

Posted 08 December 2006 - 09:45 AM

rc69 thank you so much:D
My problem is solved!
Thanks so much, you really helped me out!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users