tomfmason Posted August 5, 2006 Share Posted August 5, 2006 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 https://forums.phpfreaks.com/topic/16602-help-with-function/ Share on other sites More sharing options...
hackerkts Posted August 5, 2006 Share Posted August 5, 2006 Change[code]function newUser() {[/code]To[code]function newUser($username) {[/code] Link to comment https://forums.phpfreaks.com/topic/16602-help-with-function/#findComment-69559 Share on other sites More sharing options...
tomfmason Posted August 5, 2006 Author Share Posted August 5, 2006 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 Mastersinclude "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 5Warning: fopen(C:/windows/system32/dns/etc/users/test/masters.conf) [function.fopen]: failed to open stream: File exists in function_test.php on line 11Warning: fwrite(): supplied argument is not a valid stream resource in function_test.php on line 13Warning: fclose(): supplied argument is not a valid stream resource in function_test.php on line 14The function did not work[/code] Link to comment https://forums.phpfreaks.com/topic/16602-help-with-function/#findComment-69564 Share on other sites More sharing options...
extrovertive Posted August 5, 2006 Share Posted August 5, 2006 well, why not make $username global?$username = "test";function newUser(){global $username;...} Link to comment https://forums.phpfreaks.com/topic/16602-help-with-function/#findComment-69568 Share on other sites More sharing options...
tomfmason Posted August 5, 2006 Author Share Posted August 5, 2006 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 https://forums.phpfreaks.com/topic/16602-help-with-function/#findComment-69573 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.