ballhogjoni Posted August 31, 2011 Share Posted August 31, 2011 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 :-\ Link to comment https://forums.phpfreaks.com/topic/246078-cant-write-to-a-file-with-the-contents-of-another-file/ Share on other sites More sharing options...
Rifts Posted August 31, 2011 Share Posted August 31, 2011 which part isnt working? saving the contents to a var or saving the new file? Link to comment https://forums.phpfreaks.com/topic/246078-cant-write-to-a-file-with-the-contents-of-another-file/#findComment-1263769 Share on other sites More sharing options...
ballhogjoni Posted August 31, 2011 Author Share Posted August 31, 2011 saving the contents to the var. its like the file pointer is at the end of the file or something weird. Link to comment https://forums.phpfreaks.com/topic/246078-cant-write-to-a-file-with-the-contents-of-another-file/#findComment-1263770 Share on other sites More sharing options...
ballhogjoni Posted August 31, 2011 Author Share Posted August 31, 2011 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); Link to comment https://forums.phpfreaks.com/topic/246078-cant-write-to-a-file-with-the-contents-of-another-file/#findComment-1263785 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.