kokopoko Posted August 10, 2007 Share Posted August 10, 2007 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/64185-solved-confused-trying-to-open-and-write-to-files/ Share on other sites More sharing options...
teng84 Posted August 10, 2007 Share Posted August 10, 2007 try to check if the $sidebar, has a value Quote Link to comment https://forums.phpfreaks.com/topic/64185-solved-confused-trying-to-open-and-write-to-files/#findComment-319946 Share on other sites More sharing options...
kokopoko Posted August 10, 2007 Author Share Posted August 10, 2007 $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 Quote Link to comment https://forums.phpfreaks.com/topic/64185-solved-confused-trying-to-open-and-write-to-files/#findComment-319947 Share on other sites More sharing options...
teng84 Posted August 10, 2007 Share Posted August 10, 2007 before working on something always make sure that your on the right track so do some verification try if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } Quote Link to comment https://forums.phpfreaks.com/topic/64185-solved-confused-trying-to-open-and-write-to-files/#findComment-319951 Share on other sites More sharing options...
kokopoko Posted August 10, 2007 Author Share Posted August 10, 2007 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/64185-solved-confused-trying-to-open-and-write-to-files/#findComment-319968 Share on other sites More sharing options...
kokopoko Posted August 10, 2007 Author Share Posted August 10, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/64185-solved-confused-trying-to-open-and-write-to-files/#findComment-320017 Share on other sites More sharing options...
teng84 Posted August 10, 2007 Share Posted August 10, 2007 try to look here http://www.php.net/manual/en/function.fwrite.php Quote Link to comment https://forums.phpfreaks.com/topic/64185-solved-confused-trying-to-open-and-write-to-files/#findComment-320019 Share on other sites More sharing options...
kokopoko Posted August 10, 2007 Author Share Posted August 10, 2007 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/64185-solved-confused-trying-to-open-and-write-to-files/#findComment-320022 Share on other sites More sharing options...
teng84 Posted August 10, 2007 Share Posted August 10, 2007 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); Quote Link to comment https://forums.phpfreaks.com/topic/64185-solved-confused-trying-to-open-and-write-to-files/#findComment-320025 Share on other sites More sharing options...
kokopoko Posted August 10, 2007 Author Share Posted August 10, 2007 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/64185-solved-confused-trying-to-open-and-write-to-files/#findComment-320039 Share on other sites More sharing options...
teng84 Posted August 10, 2007 Share Posted August 10, 2007 Quote Link to comment https://forums.phpfreaks.com/topic/64185-solved-confused-trying-to-open-and-write-to-files/#findComment-320042 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.