Jump to content

Recommended Posts

I am have problems with a php file I'm trying to write.  It goes bad when I try to rename a file and open index.php and replace a string with another one.  I get the error Warning: fwrite(): 3 is not a valid stream resource in run.php on line 10

 

Warning: fclose(): 3 is not a valid stream resource in run.php on line 11

 

I have no idea what 3 is that the errors are referring to.  Can anyone see what is wrong?

 

<?php
$lines = file('keywords.txt');
$sidebar = fopen("sidebar.txt", "a");
foreach ($lines as $line_num => $line)
{
//for each keyword create <li><a href="keywordwithnospaces.php">kw</a></li> in sidebar.txt
    $nospaces=str_replace (" ", "", $line);
    $newfile=$nospaces.'.php';
    $Data='<li><a href="'.$newfile.'">'.$line.'</a></li>';
    fwrite($sidebar, $Data);
    fclose($sidebar);
//for each keyword open index.php search for text replaceme and change replaceme to keyword and save file as keywordnospaces.php
    $template = 'index.php';
   if (!copy($template, $newfile))
   {
        echo "failed to copy $file...\n";
    }
    $templatefile=fopen($newfile,"w+");
    $fix=str_replace("replaceme", $line, $templatefile);
   //fclose(templatefile);
}
?>

$sidebar is resource id #3.  So it's $sidebar that is giving the '3 is not a valid stream resource' error message.

 

For the first word I pull out of keywords.txt, the $Data is written to $sidebar.  But no other keyword pulled from keywords.txt works after that.

 

Christine

Here is the code now.  It is working for every keyword till the very last lines where I want to do a string replace in index.php.  It just completely wipes out index.php and does not a string replace.  What is wrong?

 

<?php
$lines = file('keywords.txt');
$sidebar = fopen('sidebar.txt', 'a');
$filename='index.php';
foreach ($lines as $line_num => $line)
{
//for each keyword create <li><a href="kwnospace.php">kw</a></li> in sidebar.txt
    //echo $line;
    //echo "<Br><Br>";
    $line=trim($line);
    $nospaces=str_replace (" ", "", $line);
    $nospaces=trim($nospaces);
    $newfile=$nospaces.'.php';
    $Data='<li><a href="'.$newfile.'">'.$line.'</a></li>';
    fwrite($sidebar, $Data);
    //fclose($sidebar);
//for each keyword open index.php search for text replaceme and change replaceme to kw and save file as kwnospace.php

   //if (!copy($template, $newfile))
   //{
   //     echo "failed to copy $file...\n";
   // }
   if (!$templatefile = fopen($filename, 'w+'))
   {
         echo "Cannot open file ($filename)";
         exit;
    }

    $fix=str_replace('replaceme', $line, $templatefile);
   //fclose(templatefile);
}
?>

Now I need to get the bottom part to work.  I have the string replaced data in an array.  I want to print the contents of the array into a new file.  I have PHP 4 so I can't use

file_put_contents($newfile,$fixed) or die("Cannot write file"); 

 

How do I get the data in array $fixed printed out into $newfile?

When I echo $indexfile it shows Array.  Now $indexfile was supposed to contain the entire data of a file.  So I think I need to echo each item in the array $indexfile.  I'll use a for loop, but is there an easier way?

 

<?php
$lines = file('keywords.txt');
$sidebar = fopen('sidebar.txt', 'a');

foreach ($lines as $line_num => $line)
{
//for each keyword create <li><a href="kwnospace.php">kw</a></li> in sidebar.txt
    //echo $line;
    //echo "<Br><Br>";
    $line=trim($line);
    $nospaces=str_replace (" ", "", $line);
    $nospaces=trim($nospaces);
    $newfile=$nospaces.'.php';
    $Data='<li><a href="'.$newfile.'">'.$line.'</a></li>';
    fwrite($sidebar, $Data);
    //fclose($sidebar);
//for each keyword open index.php search for text replaceme and change replaceme to kw and save file as kwnospace.php

   //if (!copy($template, $newfile))
   //{
   //     echo "failed to copy $file...\n";
   // }
   $indexfile=file('index.php');
   //if (!$templatefile = fopen($filename, 'w+'))
   //{
   //      echo "Cannot open file ($filename)";
   //      exit;
   // }
   echo $indexfile;
    $fixed=str_replace('replaceme', $line, $indexfile);
    //save $fixed as $nospaces file name
    echo $fixed;
    //file_put_contents($newfile,$fixed) or die("Cannot write file");

    //fopen($newfile,'w')or die("Cannot open newfile");

    if (!$handle = fopen($newfile, 'w')) {
         echo "Cannot open file ($newfile)";
         exit;
    }

    // Write the fixed data to our opened file.
    if (fwrite($handle, $fixed) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    fclose($handle);



   //fclose(templatefile);
}
?>

you have to access the index of each array for you to do some replacement you can use

 

for each just like what you did in the first loop you have

 

foreach($indexfile as $key => $value)

    $fixed=str_replace('replaceme', $line, $value);

 

 

Yup!  I just figured it out right before your reply.

 

<?php
$lines = file('keywords.txt');
$indexfile=file('index.php');
$sidebar = fopen('sidebar.txt', 'a');

foreach ($lines as $line_num => $line)
{
//for each keyword create <li><a href="kwnospace.php">kw</a></li> in sidebar.txt

    $line=trim($line);
    $nospaces=str_replace (" ", "", $line);
    $nospaces=trim($nospaces);
    $newfile=$nospaces.'.php';
    $Data='<li><a href="'.$newfile.'">'.$line.'</a></li>';
    fwrite($sidebar, $Data);
    $keyword=$line;
//for each keyword open index.php search for text replaceme and change replaceme to kw and save file as kwnospace.php

   foreach($indexfile as $line_num => $line)
   {
        $fixed=str_replace('replaceme', $keyword, $line);

       //save $fixed as $nospaces file name
         if (!$handle = fopen($newfile, 'a')) {
         echo "Cannot open file ($newfile)";
         exit;
            }

    // Write the fixed data to our opened file.
    if (fwrite($handle, $fixed) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
            }

    fclose($handle);
    }


   //fclose(templatefile);
}
?>

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.