fopen and fwrite to write first file's content to new one

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

fopen and fwrite to write first file's content to new one

Post 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.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: fopen and fwrite to write first file's content to new one

Post 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.
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: fopen and fwrite to write first file's content to new one

Post 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
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: fopen and fwrite to write first file's content to new one

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: fopen and fwrite to write first file's content to new on

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