Error with file script

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
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Error with file script

Post by nigma »

I have a list of files that need to be edited. The list is inside the file called "filelist.txt". I am trying to make a program that opens "filelist.txt" and then opens up every filename inside "filelist.txt" and creates a copy of the file after a certain string has been changed.

Here is my php code:

Code: Select all

<?php

	$filename = "filelist.txt";
	$filelist = fopen( $filename, "r") or die("Cannot open $filename for reading.");
	while (!feof($filelist))
	&#123;
		$fte = fgets($filelist, 1024);
		$file = fopen($fte, "r") or die("Cannot open $file for reading and editing.");
			while(!feof($file))
			&#123;
				$lte = fgets($file, 1024);
				$newFile = fopen("newFiles/$fte", "a");
				fwrite($newFile, ereg_replace("layout.css","style.css",$lte));
			&#125;
		print "File $fte has been created and edited!";
	&#125;

	print "<h1>Done!</h1>";
?>
and this is the error I get:
Warning: fopen(apply.html ) [function.fopen]: failed to create stream: Invalid argument in C:\Program Files\Apache Group\Apache2\mc0\fileedit.php on line 8
Cannot open for reading and editing.

Now this code works but only if there is one filename inside "filelist.txt".
User avatar
Kim
Forum Newbie
Posts: 6
Joined: Sun May 18, 2003 9:36 pm

Re: Error with file script

Post by Kim »

try this
$filelist = fopen( $filename, "w+") or die("Cannot open $filename for reading.");
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

What? Look at this, now I get this error with this code.

Error: Parse error: parse error, unexpected T_VARIABLE in C:\Program Files\Apache Group\Apache2\mc0\fileedit.php on line 4

Code:

Code: Select all

<?php

	$filename = "filelist.txt"
	$filelist = fopen( $filename, "w+") or die("Cannot open $filename for reading.");
	while (!feof($filelist))
	&#123;
		$fte = fgets($filelist, 1024);
		$file = fopen($fte, "r") or die("Cannot open $file for reading and editing.");
			while(!feof($file))
			&#123;
				$lte = fgets($file, 1024);
				$newFile = fopen("newFiles/$fte", "a");
				fwrite($newFile, ereg_replace("layout.css","style.css",$lte));
			&#125;
		print "File $fte has been created and edited!";
	&#125;

	print "<h1>Done!</h1>";
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

First thing you should always check is if any semi-colons have been missed off the end of lines (hint, hint).

Mac
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

caution: http://www.php.net/manual/en/function.fopen.php
'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
Post Reply