Hi,
i have a piece of code that reads a csv list.
For instance:
1,index.html
2,linux.html
I read these in an array and then i want to use the filename
(2nd array element) to make a file and write something in it.
However, i always get the error:
Warning: fopen(index.htm l ): failed to open stream: Invalid argument in
E:\DATA\startpa ge\generate.php on line 34
Cannot open file
The code that gives problems is this and more specificaly the titel var
if (!$file_handle = fopen($titel,'a ')) { echo "Cannot open file
".$titel; die; }
If i for instance change this $titel to "index.html " then i do not get
warnings.
So it probably has to do with how $titel is presented.
I tried wrapping $titel in double qoute, that doesn't work either.
I tried wrapping $titel in single qoutes but that off course uses $titel as
the
name of the file instead of the content of the variable.
How can i make this work?
Code:
====
<?php
$PAGES = "pages.txt" ;
$data = 'This ';
$data .= 'is a ';
$data .= 'string.';
/* open pages parse them and make the pages */
$file = file($PAGES);
foreach ($file as $line) {
$arTemp = explode(",",$li ne);
$aPages[$arTemp[0]] = $arTemp[1];
echo "Making ".$arTemp[1]." with id ".$arTemp[0]."\n";
unset($arTemp);
}
foreach ($aPages as $sleutel => $titel) {
print "Huidige waarde van \$aPages: $sleutel, $titel.\n";
/* write file*/
if (!$file_handle = fopen($titel,'a ')) { echo "Cannot open file
".$titel; die; }
if (!fwrite($file_ handle, $data)) { echo "Cannot write to file
".$titel; die; }
fclose($file_ha ndle);
}
?>
Regards,
flupke
i have a piece of code that reads a csv list.
For instance:
1,index.html
2,linux.html
I read these in an array and then i want to use the filename
(2nd array element) to make a file and write something in it.
However, i always get the error:
Warning: fopen(index.htm l ): failed to open stream: Invalid argument in
E:\DATA\startpa ge\generate.php on line 34
Cannot open file
The code that gives problems is this and more specificaly the titel var
if (!$file_handle = fopen($titel,'a ')) { echo "Cannot open file
".$titel; die; }
If i for instance change this $titel to "index.html " then i do not get
warnings.
So it probably has to do with how $titel is presented.
I tried wrapping $titel in double qoute, that doesn't work either.
I tried wrapping $titel in single qoutes but that off course uses $titel as
the
name of the file instead of the content of the variable.
How can i make this work?
Code:
====
<?php
$PAGES = "pages.txt" ;
$data = 'This ';
$data .= 'is a ';
$data .= 'string.';
/* open pages parse them and make the pages */
$file = file($PAGES);
foreach ($file as $line) {
$arTemp = explode(",",$li ne);
$aPages[$arTemp[0]] = $arTemp[1];
echo "Making ".$arTemp[1]." with id ".$arTemp[0]."\n";
unset($arTemp);
}
foreach ($aPages as $sleutel => $titel) {
print "Huidige waarde van \$aPages: $sleutel, $titel.\n";
/* write file*/
if (!$file_handle = fopen($titel,'a ')) { echo "Cannot open file
".$titel; die; }
if (!fwrite($file_ handle, $data)) { echo "Cannot write to file
".$titel; die; }
fclose($file_ha ndle);
}
?>
Regards,
flupke
Comment