Jump to content

Can't write to a file with the contents of another file


ballhogjoni

Recommended Posts

I need some help...I want to read the contents of a file into a variable and then insert/write that content into another file.

 

$handle = fopen($create_mysql_db_url, "rb");
  $contents = fread($handle, filesize($create_mysql_db_url));
  fclose($handle);

  $userFileName = $_POST['mysqluser'] . time() . ".php.txt";
  $fhandle = fopen($userFileName,"wb");
  fwrite($fhandle, $contents);
  fclose($fhandle);

  $fh = fopen($userFileName,"wb");
  $contents = fread($fh, filesize($userFileName));

  $content = preg_replace("/###USERNAME###/", $_POST['mysqluser'], $contents);
  $content = preg_replace("/###PASSWORD###/", $_POST['mysqlpass'], $content);

  fwrite($fh, $content);
  fclose($fh);

 

please help  :-\

I got it...first i was trying to copy a php file...thats a no no or atleast with the <?php tag.

 

my fix and what is working:

$output="";
  $file = fopen($create_mysql_db_url, "r");
  while(!feof($file)) {
    //read file line by line into variable
    $output = $output . fgets($file, 4096);
  }
  fclose ($file);

  $userFileName = $_POST['mysqluser'] . time() . ".php.txt";
  $file = fopen($userFileName, "w+");
  $output = preg_replace("/###USERNAME###/", $_POST['mysqluser'], $output);
  $output = preg_replace("/###PASSWORD###/", $_POST['mysqlpass'], $output);
  fputs($file, "<?php" . $output);
  fclose ($file);

Archived

This topic is now archived and is closed to further replies.

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