Page 1 of 1

Error with file script

Posted: Sun May 18, 2003 10:24 pm
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".

Re: Error with file script

Posted: Sun May 18, 2003 10:34 pm
by Kim
try this
$filelist = fopen( $filename, "w+") or die("Cannot open $filename for reading.");

Posted: Sun May 18, 2003 10:38 pm
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>";
?>

Posted: Mon May 19, 2003 3:12 am
by twigletmac
First thing you should always check is if any semi-colons have been missed off the end of lines (hint, hint).

Mac

Posted: Mon May 19, 2003 3:53 am
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.