Jump to content

how to create php file with the php code - with fwrite


Recommended Posts

// 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..

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;

<?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..

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

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...

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.