PHP Writing with fopen

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
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

PHP Writing with fopen

Post by waradmin »

im working on an automated blog system (like xanga) where users register, and it creates a directory, mysql database, and then copies the files to their directory for their blog. The blog software was written by me, and has a required config.php file that needs to be setup for each persons blog, and it goes in their directory.

So here is an example of what needs to be in the config file:

Code: Select all

#Edit this area to set your admin username and password

$adminuser  = "admin"; #administrator username
$adminpass  = "admin"; #administrator password
$adminknown = "Steve"; #Who the administrator is known as (aka name)

//End of Admin edit area

# Edit this area to set the title of your blog, your email, etc

$title      = "Simpleblog"; #The title of your blog appears in Title Bar for web browser
$blogname   = "steve's blog"; #Name of blog as you want to appear in the page header
$email      = "admin@open-server.org"; #Your email address
$name       = "steve"; #Your name or nick name
$perpage    = "50000"; #Number of entrys for the homepage
$absolute   = "http://www.open-server.org/"; #absolute location of blog

//End general information area
Ok, so the user will fill out a registration form for each one of those variables. Now i need to write that information into a blank config.php file so it looks like the above. The problem is that when i use fopen with the following code:

Code: Select all

<?php
$filename = 'config2.php';
$somecontent = "$dbuser = \"root\"; $dbserver	= \"localhost\";";

if (is_writable($filename)) {

   if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
   }

   if (fwrite($handle, $somecontent) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
   
   echo "Success, wrote ($somecontent) to file ($filename)";
   
   fclose($handle);

} else {
   echo "The file $filename is not writable";
}
?>
It doesnt write the $ variables. So it only places the = sign and after into the file. How do i get the fopen function to write the whole variable and not just what is after the variable.

For example: Currently it says $dbuser = root but when it writes to the file it only writes = root

Thanks in advance.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

You're supposed to use single quotes... let me tinker with it for a minute I'll see if I can fix it.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

try this:

Code: Select all

$somecontent ='$dbuser = "root"; $dbserver    = "localhost";';
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

That worked thank you.

Next roadblock ive reached is that the user enters a desired username in the signup form. A directory is created with that username, was well as a database. The username is set as $name

So how would i get the config file to set the dbname field (defines the name of the database) to echo the $name that the user entered on their form. I cant seem to get the value of $name to display in the file, just the "$name"

So how would i get the value of $name to display and not just $name?

Thanks.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

post the code, but probably something like this...

Code: Select all

$somecontent ='$dbuser = "'.$entryone.'"; $dbserver    = "'.$entrytwo.'";';
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

Thanks for the help that worked perfectly.

I have a beta up in action if anyone wants to try out my blog service: http://blog.open-server.org/

Give feedback if you want. I could use it. Thanks.
egmax
Forum Newbie
Posts: 5
Joined: Mon Nov 07, 2005 10:19 pm
Location: &#20013;&#22269;CHINA

Post by egmax »

e.g.

$msg = "\$v1=0; \$v2=1;";

:roll:
Post Reply