fopen and filename as array element (code included)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • flupke

    fopen and filename as array element (code included)

    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


  • CountScubula

    #2
    Re: fopen and filename as array element (code included)

    Just a thought, is that directorys permission set to allow you to write a
    file?

    --
    Mike Bradley
    http://www.gzentools.com -- free online php tools
    "flupke" <flupke@nonexis tingdomain.com> wrote in message
    news:eWnLb.3964 $qK.289921@phob os.telenet-ops.be...[color=blue]
    > 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[/color]
    as[color=blue]
    > 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
    >
    >[/color]


    Comment

    • Pedro Graca

      #3
      Re: fopen and filename as array element (code included)

      flupke wrote:
      (snip)[color=blue]
      > /* open pages parse them and make the pages */
      > $file = file($PAGES);
      >
      > foreach ($file as $line) {
      > $arTemp = explode(",",$li ne);
      > $aPages[$arTemp[0]] = trim($arTemp[1]);[/color]
      // _______________ ______________^ ^^^^__________^ __

      Remove the newline file() leaves in place
      --
      --= my mail box only accepts =--
      --= Content-Type: text/plain =--
      --= Size below 10001 bytes =--

      Comment

      • flupke

        #4
        Re: fopen and filename as array element (code included)

        "Pedro Graca" <hexkid@hotpop. com> wrote in message
        news:btlu4v$8lc u8$1@ID-203069.news.uni-berlin.de...[color=blue]
        > flupke wrote:
        > (snip)[color=green]
        > > /* open pages parse them and make the pages */
        > > $file = file($PAGES);
        > >
        > > foreach ($file as $line) {
        > > $arTemp = explode(",",$li ne);
        > > $aPages[$arTemp[0]] = trim($arTemp[1]);[/color]
        > // _______________ ______________^ ^^^^__________^ __
        >
        > Remove the newline file() leaves in place[/color]

        That works like a charm!
        Thank you :)

        flupke


        Comment

        Working...