Jump to content

help with function.


tomfmason

Recommended Posts

I already have a script that does what I want but I now want to clean it up abit. So I thought I would write some functions and call them in my script. Well write now I am just testing it. The problem that I am having is that the uername is not being passed to the function.


Here is what I am testing so far.
[code]<?php
$username = "test";
function newUser() {
  //creates a directory for masters.conf and zone files
  mkdir('C:/windows/system32/dns/etc/users/' . $username . '');
  if (!mkdir) {
      echo "unable to create the needed directory";
  }
  //creates a masters.conf in the new directory
  $filename = 'C:/windows/system32/dns/etc/users/' . $username . '/masters.conf';
  $handle = fopen($filename, 'x+');
  $content = "#Begin Masters";
  fwrite($handle, $content);
  fclose($handle);
 
  //adds a new line in the main masters.conf
  $file = 'C:/windows/system32/dns/etc/masters.conf';
  $fp = fopen($file, "r+b");
  $layout = 'include "' . $username . '/masters.conf";';
 
  while (!feof($fp)) {
      $line = trim(fgets($fp, 1024));
  if ($line == '#Begin Masters') {
      $fpos = ftell($fp);
  $rest = fread($fp, filesize($file));
 
  fseek($fp, $fpos, SEEK_SET);
  fwrite($fp, "\n\n");
  fwrite($fp, $layout);
  fwrite($fp, $rest);
}
  }
  fclose($fp);  
}
newuser();
if(!newuser()) {
  echo "The function did not work";
}else{
  echo "The directory $username was created sucessfuly";
}       
?>
[/code]

So basicly I want to pass the usersname to the function.

Any suggestions as to how to do this or a better way would be great.

Thanks,
Tom
Link to comment
Share on other sites

I tried this

[code]<?php
$username = "test";
function newUser() {
  //creates a directory for masters.conf and zone files
  mkdir('C:/windows/system32/dns/etc/users/' . $GLOBALS['username'] . '');
  if (!mkdir) {
      echo "unable to create the needed directory";
  }
  //creates a masters.conf in the new directory
  $filename = 'C:/windows/system32/dns/etc/users/' . $GLOBALS['username'] . '/masters.conf';
  $handle = fopen($filename, 'x+');
  $content = "#Begin Masters";
  fwrite($handle, $content);
  fclose($handle);
 
  //adds a new line in the main masters.conf
  $file = 'C:/windows/system32/dns/etc/masters.conf';
  $fp = fopen($file, "r+b");
  $layout = 'include "' . $GLOBALS['username'] . '/masters.conf";';
 
  while (!feof($fp)) {
     $line = trim(fgets($fp, 1024));
 if ($line == '#Begin Masters') {
     $fpos = ftell($fp);
 $rest = fread($fp, filesize($file));
 
 fseek($fp, $fpos, SEEK_SET);
 fwrite($fp, "\n\n");
 fwrite($fp, $layout);
 fwrite($fp, $rest);
}
 }
 fclose($fp);  
}
newuser();
if(!newuser()) {
  echo "The function did not work";
}else{
  echo "The directory $username was created sucessfuly";
}        
?>[/code]

It creates the directory just fine but it repeats the [code=php:0]$layout = 'include "' . $GLOBALS['username'] . '/masters.conf";';[/code] twice on the same line like this.

[code]#Begin Masters
include "test/masters.conf";

include "test/masters.conf";[/code]

when I try to run this it creates everything that it is supposed to but it still returns the following errors.

[code]Warning: mkdir() [function.mkdir]: File exists in function_test.php on line 5

Warning: fopen(C:/windows/system32/dns/etc/users/test/masters.conf) [function.fopen]: failed to open stream: File exists in function_test.php on line 11

Warning: fwrite(): supplied argument is not a valid stream resource in function_test.php on line 13

Warning: fclose(): supplied argument is not a valid stream resource in function_test.php on line 14
The function did not work[/code]
Link to comment
Share on other sites

I got the globals issue figured out but now I have another issue.

I changed

[code=php:0]newuser();
if(!newuser()) {
  echo "The function did not work";
}else{
  echo "The directory $username was created sucessfuly";
}[/code]


To


[code=php:0]$test = newUser();
if(!$test) {
  echo "The function did not work";
}else{
  echo "The directory $username was created sucessfuly";
}[/code]

I did this because I realized that the first one was calling the function twice. I no longer get the all the error messages but it still echos [b]The function did not work[/b]. Everything works as it should but I still get that message. I guess that I could just omit this part of the script. I would rather know why it is doing this?

Any suggestions would be great.

Thanks for the help,
Tom
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.