Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    nst
    nst is offline
    New Coder
    Join Date
    Jul 2005
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts

    problem with function file

    I have a file_list.txt which contains 3 lines

    Code:
    20050104.txt
    20050105.txt
    20050107.txt
    Each of the lines are the contents of a directory. First I get the filenames and then I want to get the content of each file by using file($filename[$i]), but I get the following error

    Code:
    Warning: file(20050104.txt ): failed to open stream: Invalid argument in .....
    How can this be done?

    Code:
    <?php
    
    $filename = file('file_list.txt');
    $l_count = count($filename);
    
    for($i = 0; $i < $l_count; $i++)
    {
       echo $filename[$i]."<BR>";
       $filecontent = file($filename[$i]);
    }
    
    
    ?>

  • #2
    New Coder
    Join Date
    Jan 2006
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Maybe you need quotation marks?
    You could try that

  • #3
    nst
    nst is offline
    New Coder
    Join Date
    Jul 2005
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Zegg90
    Maybe you need quotation marks?
    You could try that
    Where to put them exactly?

  • #4
    Regular Coder
    Join Date
    Jan 2006
    Location
    Preston, Lancashire, England
    Posts
    285
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it could also be you aren't stripping the guff at the end of the line.
    basically as you have the file names on new lines changes are there is a new line code at the end of the text. Also by looks of it there is a space at the end. file(20050104.txt )
    what you could to is
    PHP Code:
    $filecontent file(rtrim($filename[$i])); 

  • #5
    nst
    nst is offline
    New Coder
    Join Date
    Jul 2005
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Right! Actually 'trim' worked too.

    $filecontent = file(trim($filename[$i]));

  • #6
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Hi nst,

    here's my two cents worth: if you are simply reading files from a local directory, have you considered using scandir? My appologies if that isn't what you want, just thought I'd mention it!

    Mike.
    Die Welt ist ein Irrenhaus und hier ist die Zentrale!

  • #7
    nst
    nst is offline
    New Coder
    Join Date
    Jul 2005
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlse
    Hi nst,

    here's my two cents worth: if you are simply reading files from a local directory, have you considered using scandir? My appologies if that isn't what you want, just thought I'd mention it!

    Mike.
    Didn't think of that. It sounds a good idea, thank you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •