vamsig Posted October 30, 2008 Share Posted October 30, 2008 // hiii mkdir ("./$userid/", 0755, true); $myFile = "./$userid/index.php"; $fh = fopen($myFile, 'w') or die("can't open file"); $filename = "./$userid/index.php"; $somecontent = " <?php $con = mysql_connect('localhost','root',''); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("signup", $con); $result = mysql_query("SELECT * FROM user_signup where userid='$userid'"); while($row = mysql_fetch_array($result)) { echo $row['email'] . " " . $row['name']; echo "<br />"; } mysql_close($con); ?> "; // Let's make sure the file exists and is writable first. //if (is_writable($filename)) { // In our example we're opening $filename in append mode. // The file pointer is at the bottom of the file hence // that's where $somecontent will go when we fwrite() it. if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success"; fclose($handle); //hiii The above code creates a "directory" and index.php file need to insert the php database connection code into the index.php file Example: How Create config.php file with code Thanking you.. Quote Link to comment https://forums.phpfreaks.com/topic/130716-how-to-create-php-file-with-the-php-code-with-fwrite/ Share on other sites More sharing options...
trq Posted October 30, 2008 Share Posted October 30, 2008 The big question here is why? There are better ways of going about this. Quote Link to comment https://forums.phpfreaks.com/topic/130716-how-to-create-php-file-with-the-php-code-with-fwrite/#findComment-678352 Share on other sites More sharing options...
vamsig Posted October 30, 2008 Author Share Posted October 30, 2008 what is that way... Quote Link to comment https://forums.phpfreaks.com/topic/130716-how-to-create-php-file-with-the-php-code-with-fwrite/#findComment-678355 Share on other sites More sharing options...
trq Posted October 30, 2008 Share Posted October 30, 2008 You simply have page that serves up all users data, then use apache's mod_rewrite to rewrite the url so users can access there page via http://yoursite.com/username. Quote Link to comment https://forums.phpfreaks.com/topic/130716-how-to-create-php-file-with-the-php-code-with-fwrite/#findComment-678358 Share on other sites More sharing options...
bobbinsbro Posted October 30, 2008 Share Posted October 30, 2008 and in case you ever want to do anything like this again, you'd be better off changing this: $somecontent = " <?php $con = mysql_connect('localhost','root',''); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("signup", $con); $result = mysql_query("SELECT * FROM user_signup where userid='$userid'"); while($row = mysql_fetch_array($result)) { echo $row['email'] . " " . $row['name']; echo "<br />"; } mysql_close($con); ?> "; with this: $somecontent = <<<EOT <?php $con = mysql_connect('localhost','root',''); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("signup", $con); $result = mysql_query("SELECT * FROM user_signup where userid='$userid'"); while($row = mysql_fetch_array($result)) { echo $row['email'] . " " . $row['name']; echo "<br />"; } mysql_close($con); ?> EOT; Quote Link to comment https://forums.phpfreaks.com/topic/130716-how-to-create-php-file-with-the-php-code-with-fwrite/#findComment-678366 Share on other sites More sharing options...
vamsig Posted October 30, 2008 Author Share Posted October 30, 2008 <?php mkdir ("./newdir1/", 0755, true); $myFile = "./newdir1/index.php"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = <<<EOT <?php $con = mysql_connect('localhost','root',''); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("signup", $con); $result = mysql_query("SELECT * FROM user_signup where userid='$userid'"); while($row = mysql_fetch_array($result)) { echo $row['email'] . " " . $row['name']; echo "<br />"; } mysql_close($con); ?> EOT; fwrite($fh, $stringData); fclose($fh); ?> its not working Please check and give me a solution.. Quote Link to comment https://forums.phpfreaks.com/topic/130716-how-to-create-php-file-with-the-php-code-with-fwrite/#findComment-678412 Share on other sites More sharing options...
sasa Posted October 30, 2008 Share Posted October 30, 2008 try // hiii mkdir ("./$userid/", 0755, true); $myFile = "./$userid/index.php"; $fh = fopen($myFile, 'w') or die("can't open file"); $filename = "./$userid/index.php"; $somecontent = ' <?php $con = mysql_connect(\'localhost\',\'root\',''); if (!$con) { die(\'Could not connect: \' . mysql_error()); } mysql_select_db("signup", $con); $result = mysql_query("SELECT * FROM user_signup where userid=\'$userid\'"); while($row = mysql_fetch_array($result)) { echo $row[\'email\'] . " " . $row[\'name\']; echo "<br />"; } mysql_close($con); ?> '; // Let's make sure the file exists and is writable first. //if (is_writable($filename)) { // In our example we're opening $filename in append mode. // The file pointer is at the bottom of the file hence // that's where $somecontent will go when we fwrite() it. if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success"; fclose($handle); //hiii Quote Link to comment https://forums.phpfreaks.com/topic/130716-how-to-create-php-file-with-the-php-code-with-fwrite/#findComment-678420 Share on other sites More sharing options...
vamsig Posted October 30, 2008 Author Share Posted October 30, 2008 not working... Quote Link to comment https://forums.phpfreaks.com/topic/130716-how-to-create-php-file-with-the-php-code-with-fwrite/#findComment-678430 Share on other sites More sharing options...
bobbinsbro Posted October 30, 2008 Share Posted October 30, 2008 i figured out why what i (and probably sasa too) gave you didn't work: if there're <?php ?> tags in the HEREDOC (or the quotes ' ' in sasa's version) it won't work. so this works: <?php $stringData = <<<EOT \$con = mysql_connect('localhost','root',''); if (!\$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("signup", \$con); \$result = mysql_query("SELECT * FROM user_signup where userid='\$userid'"); while(\$row = mysql_fetch_array(\$result)) { echo \$row['email'] . " " . \$row['name']; echo "<br />"; } mysql_close(\$con); EOT; ?> to add the <?php ?> to the file after it's created... Quote Link to comment https://forums.phpfreaks.com/topic/130716-how-to-create-php-file-with-the-php-code-with-fwrite/#findComment-678438 Share on other sites More sharing options...
trq Posted October 30, 2008 Share Posted October 30, 2008 Still, I don't see the point in creating a new file for each of your users. Quote Link to comment https://forums.phpfreaks.com/topic/130716-how-to-create-php-file-with-the-php-code-with-fwrite/#findComment-678507 Share on other sites More sharing options...
play_ Posted October 30, 2008 Share Posted October 30, 2008 Still, I don't see the point in creating a new file for each of your users. vamsig you should pay attention to this. What happens if you decide to update? You're gonna go through each user's index file and make the changes? =S Quote Link to comment https://forums.phpfreaks.com/topic/130716-how-to-create-php-file-with-the-php-code-with-fwrite/#findComment-678675 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.