Page 1 of 1
fopen and fwrite to write first file's content to new one
Posted: Wed Jun 03, 2009 12:11 pm
by lovelf
Hi, I'm trying to write the contents of one file to a new one.
Code: Select all
$fpv = fopen("filea.php", 'a');
$fp = fopen("fileb.php", 'w');
fwrite($fp, "$fpv");
fclose($fp);
fclose($fpv);
File b isn't getting a contents this way.
Re: fopen and fwrite to write first file's content to new one
Posted: Wed Jun 03, 2009 12:36 pm
by mikemike
This won't work. $fpv isn't the contents of filea, it's simply a resource pointer to it. Check the
documentation for return values.
You'll need to
fgets() or
fread() on the file pointer first.
Post back if you're unsure.
Re: fopen and fwrite to write first file's content to new one
Posted: Wed Jun 03, 2009 1:33 pm
by lovelf
Thanks, I've managed to copy html from the first file to the second, but couldn't execute (?) PHP on it as I got its contents to be written on the new file.
Code: Select all
<html>
echo $avariable; // file a contents
</html>
The script:
Code: Select all
$avariable='hello';
$buffer='';
$handle = fopen("filea.php", "r");
while (!feof($handle)) {
$buffer.= fgets($handle, 4096);
}
$fp = fopen("fileb.html", 'w');
fwrite($fp, "$buffer");
fclose($fp);
fclose($handle);
How to write $avariable value on fileb.html set by the script with the echo statement made on filea.php
Re: fopen and fwrite to write first file's content to new one
Posted: Wed Jun 03, 2009 5:58 pm
by mikemike
Using fopen grabs the file contents as it is on the file system. If you want it pasring by php then you need to grab it via http (using curl or another method).
You could use command line PHP to do it:
Code: Select all
$variable = exec('php filea.php');
but you'll have trouble passing values to it as (I think) each word is treated as a seperate arguement within $argv[]. Probably easier to just go with the http method.
Re: fopen and fwrite to write first file's content to new on
Posted: Wed Jun 03, 2009 6:35 pm
by McInfo
You could use output buffering.
html-in.php
Code: Select all
<html>
<body>
<?php echo $htmlBody; ?>
</body>
</html>
build.php
Code: Select all
<?php
$inFile = 'html-in.php';
$outFile = 'html-out.html';
$htmlBody = '<p>Hello, World.</p>';
// Checks that the input file exists.
// Opens the output file for writing and truncates it to zero length.
if (file_exists($inFile) && ($fo = fopen($outFile, 'w'))) {
// Starts the output buffer.
ob_start();
// Reads and parses the input file like any other PHP script.
include $inFile;
// Writes the contents of the output buffer to the output file.
fwrite($fo, ob_get_contents());
// Flushes the ouput buffer so the contents are not displayed
// and turns off output buffering.
ob_end_clean();
// Closes the output file.
fclose($fo);
// Displays a link to the output file
echo '<a href="'.$outFile.'">'.$outFile.'</a>';
}
?>
Output of
build.php
Code: Select all
<a href="html-out.html">html-out.html</a>
Contents of
html-out.html
Code: Select all
<html>
<body>
<p>Hello, World.</p>
</body>
</html>
Edit: This post was recovered from search engine cache.