Insert information into file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Termina
Forum Newbie
Posts: 18
Joined: Sat Apr 10, 2004 11:17 pm

Insert information into file

Post by Termina »

I'd like to have two fields from a forum be entered into a text document (acually a php page) in certain places in the document.

Here's where I want the info to go:

Code: Select all

<CENTER>
<?
 include("layout.php");
    function add_page_content() &#123;
  // Grab info...
  $will = file_get_contents("/home/will/.plan");
  $charlie = file_get_contents("/home/charlie/.plan");
  $HoboGod = file_get_contents("/home/HoboGod/.plan");
  $pyr0t3ch = file_get_contents("/home/pyr0t3ch/.plan");
  $KalzzMod = file_get_contents("/home/KalzzMod/.plan");
  $Striker = file_get_contents("/home/Striker/.plan");
  $as2100 = file_get_contents("/home/as2100/.plan");
  $Cool_Fire = file_get_contents("/home/Cool_Fire/.plan");
  $Ziplock = file_get_contents("/home/Ziplock/.plan");
  $mickiscoole = file_get_contents("/home/mickiscoole/.plan");
  $hellstorm = file_get_contents("/home/hellstorm/.plan");
  $chronos = file_get_contents("/home/chronos/.plan");
  $DemonCat = file_get_contents("/home/DemonCat/.plan");

?>
<a href="~will">Jester:</a><?
echo("<br/>".$will."<br/><br><br>");
?>

<a href="~charlie">charlie:</a><?
echo("<br/>".$charlie."<br/><br><br>");
?>

<a href="~pyr0t3ch">pyr0t3ch:</a><?
echo("<br/>".$pyr0t3ch."<br/><br><br>");
?>

<a href="~HoboGod">HoboGod:</a><?
echo("<br/>".$HoboGod."<br/><br><br>");
?>

<a href="~KalzzMod">KalzzMod:</a><?
echo("<br/>".$KalzzMod."<br/><br><br>");
?>

<a href="~striker">Striker:</a><?
echo("<br/>".$Striker."<br/><br><br>");
?>

<a href="~as2100">as2100:</a><?
echo("<br/>".$as2100."<br/><br><br>");
?>

<a href="~chronos">chronos:</a><?
echo("<br/>".$chronos."<br/><br><br>");
?>

<a href="~Cool_Fire">Cool_Fire:</a><?
echo("<br/>".$Cool_Fire."<br/><br><br>");
?>

<a href="~Ziplock">Ziplock:</a><?
echo("<br/>".$Ziplock."<br/><br><br>");
?>

<a href="~mickiscoole">mickiscoole:</a><?
echo("<br/>".$mickiscoole."<br/><br><br>");
?>

<a href="~hellstorm">hellstorm:</a><?
echo("<br/>".$hellstorm."<br/><br><br>");
?>

<a href="~DemonCat">DemonCat:</a><?
echo("<br/>".$DemonCat."<br/><br><br>");
?>

<? &#125; //End ?>
I'd like it to enter a

Code: Select all

<a href="~username">username:</a><?
echo("<br/>".$username."<br/><br><br>");
?>
line every time a user adds an account via another page. It should always go above "<? } //End ?>", and "username" should be replaced by the username field of the document.

It should also add a

Code: Select all

$username = file_get_contents("/home/username/.plan");
line above this line:

Code: Select all

<a href="~will">Jester:</a><?
echo("<br/>".$will."<br/><br><br>");
?>
Any help you could give me would be greatly appreciated!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd write an auto-discovery function for that. Something like:

Code: Select all

$hubDir = '/home';
foreach(glob("$hubDir/*") as $filename)
&#123;
  if( is_dir($filename) && is_file("$filename/.plan") )
  &#123;
    $username = preg_replace("/\/(.*)$/","\\1",$filename);
    $filedata = file_get_contents("$filename/.plan");
    echo "<a href="~&#123;$username&#125;">$username</a><br />$filedata<br /><br />";
  &#125;
&#125;
Termina
Forum Newbie
Posts: 18
Joined: Sat Apr 10, 2004 11:17 pm

Post by Termina »

Wow, thank you very much. :)

It works nicely, only I'm a bit confused about something:

While I want to get the usernames from "/home", it's showing links to "http://sitename.com/~home/username" instead of "http://sitename.com/~username".

Is there an easy way to fix this?

I tried changing \\1 to \\5 (to remove "home\") but that makes the entire line (along with the username) disappear. Even if I just change it to \\2 it removes the whole line. =/

Thanks again!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

doh! messed up the regex.. I do that a lot when I don't check it in my IDE first.. ;)

Code: Select all

$hubDir = '/home'; 
foreach(glob("$hubDir/*") as $filename) 
&#123; 
  if( is_dir($filename) && is_file("$filename/.plan") ) 
  &#123; 
    $username = explode("/",$filename);
    $username = $username&#1111;count($username)-1];
    $filedata = file_get_contents("$filename/.plan"); 
    echo "<a href="~&#123;$username&#125;">$username</a><br />$filedata<br /><br />"; 
  &#125; 
&#125;
should take care of it..

edit: changed the regex to an explode.. easier on my brain atm.. :)
Termina
Forum Newbie
Posts: 18
Joined: Sat Apr 10, 2004 11:17 pm

Post by Termina »

I came across this script the other day (It's been awhile ^_^;), and it no longer seems to work.

When trying either script, I get a blank white page. =(

When I change <? ?> to <php? ?>, I get this:

Code: Select all

$username
$filedata

"; &#125; &#125; ?>
I'm using feyd's code from the post above this one.

At first, I thought it was a permission issue, so I chmod -R 777 /home

And no change. :(

Thanks for any help you can give me!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

<?php ... ?> not <php?
Termina
Forum Newbie
Posts: 18
Joined: Sat Apr 10, 2004 11:17 pm

Post by Termina »

Sorry, typo.

Though it SHOULDN'T need <php?, it should just use <?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you may not have short-tags on.. quite possible if you deal with xml regularly. As for the output, if you look at the page's source code, I would imagine you'd see the php code, which would mean that php did not parse that page.
Termina
Forum Newbie
Posts: 18
Joined: Sat Apr 10, 2004 11:17 pm

Post by Termina »

feyd wrote:you may not have short-tags on.. quite possible if you deal with xml regularly. As for the output, if you look at the page's source code, I would imagine you'd see the php code, which would mean that php did not parse that page.
I'm sure I do, since all my other PHP pages are <? ?>, and I don't use XML. :)

I'm copying/pasting from VIM, not from what appears on screen.

As I stated above, I get a blank page (all white). No text.

The only time I get some output is when I use <php? ?>, and then only because echo gives me a few charachters at the end.

Can you see anything wrong with the code itself?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't see anything that would cause such strange output... might need to look at your error logs to see if anything shows up there...
Post Reply