I am trying to get a script working that I made, and I am having the problem with the fopen function not working in a loop. The basic idea behind the code is shown below <?php $leech_list = fopen("leech_list.txt", "r");// open leech list if ($leech_list) { while (!feof($leech_list)) { $leech_number++; $leech_sites[$leech_number] = fgets($leech_list); // store sites to leech from in an array } } for ($x=1; $x<=$leech_number; $x++) { $leech_connection = fopen("$leech_sites[$x]", "r"); // open connection to leech site $string = fgets($leech_connection); echo $string . "<br>"; fclose ($leech_connection); } ?> PHP: I probably have some very bad conventions in the code as this is my first script besides the Hello World types. leech_list.txt contains a list of files I want to connect to, for testing purposes I have used list1.txt, list2.txt and list3.txt. Each file contains "This is some text from list (1,2 or 3 respectively). The wierd thing is that the last file in leech_list.txt always opens and closes fine. The output to the above is shown below ------------------------------- Warning: fopen(list1.txt ) [function.fopen]: failed to open stream: Invalid argument in C:\xampplite\htdocs\pgrab\loop_test.php on line 13 Warning: fgets(): supplied argument is not a valid stream resource in C:\xampplite\htdocs\pgrab\loop_test.php on line 14 Warning: fclose(): supplied argument is not a valid stream resource in C:\xampplite\htdocs\pgrab\loop_test.php on line 16 Warning: fopen(list2.txt ) [function.fopen]: failed to open stream: Invalid argument in C:\xampplite\htdocs\pgrab\loop_test.php on line 13 Warning: fgets(): supplied argument is not a valid stream resource in C:\xampplite\htdocs\pgrab\loop_test.php on line 14 Warning: fclose(): supplied argument is not a valid stream resource in C:\xampplite\htdocs\pgrab\loop_test.php on line 16 This is some text from list 3 ------------------------------ Can anyone help me understand what the problem with fopen is caused by? Thanks
It may be extra whitespace at the end of each line (the line feed) that's throwing stuff out, you need to trim the white space off the filename before inputting it into fopen.
Thanks a lot for the suggestion. It turns out that in the example I gave above, I forgot to put the trim in. However in my actual script, I was missing a $ sign in part of the trim variable. I have tested now and everything works great. I'll chuck some rep your way.