Use file() function to read a variable name, not a literal name.

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

    Use file() function to read a variable name, not a literal name.

    I know I can use the file() function to read a file into an array by
    providing a literal file name between single quotes:

    $lines = file('literal_f ile_name');

    But, what if I want file() to use a variable name?

    $variable_file_ name = "literal_file_n ame";
    $lines = file($variable_ file_name);

    Of course, that example won't work. But what will?

  • Alvaro G. Vicario

    #2
    Re: Use file() function to read a variable name, not a literal name.

    *** walterbyrd escribió/wrote (2 Aug 2006 10:49:28 -0700):
    I know I can use the file() function to read a file into an array by
    providing a literal file name between single quotes:
    >
    $lines = file('literal_f ile_name');
    >
    But, what if I want file() to use a variable name?
    From manual:

    array file ( string filename [, int use_include_pat h [, resource context]]
    )

    You need to provide a string. Period. PHP doesn't know/care how you create
    the string.

    $variable_file_ name = "literal_file_n ame";
    $lines = file($variable_ file_name);
    >
    Of course, that example won't work. But what will?
    What error are you getting?


    --
    -+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
    ++ Mi sitio sobre programación web: http://bits.demogracia.com
    +- Mi web de humor con rayos UVA: http://www.demogracia.com
    --

    Comment

    • walterbyrd

      #3
      Re: Use file() function to read a variable name, not a literal name.


      Alvaro G. Vicario wrote:
      What error are you getting?
      >
      This is what I'm using. It is copied almost verbatim from the php.net
      examples for using the file() function. One difference: I'm using a
      variable ($userfile), instead of a literal. The variable does contain a
      file name, I've checked. If I replace the variable, with a literal,
      this will work.

      // Get a file into an array. In this example we'll go through HTTP to
      get
      // the HTML source of a URL.
      $lines = file($userfile) ;

      // Loop through our array, show HTML source as HTML source; and line
      numbers too.
      foreach ($lines as $line_num =$line) {
      echo "Line #<b>{$line_num} </b: " . htmlspecialchar s($line) . "<br
      />\n";
      };

      Here is the error that I'm getting:

      Warning: Invalid argument supplied for foreach() . . .

      Comment

      • walterbyrd

        #4
        Re: Use file() function to read a variable name, not a literal name.


        Alvaro G. Vicario wrote:
        What error are you getting?
        >
        Opps. I'm sorry to have wasted your time. It was just a syntax error.
        I've fixed it.

        *sigh* I've been away from programming for too long.

        Comment

        Working...