Page 1 of 1

Problems with fopen for remote file

Posted: Mon Jun 15, 2009 11:05 pm
by atyler
Searched the forums and Googled...no bueno.

Here's my problem:

I have a list of filenames stored in a text file, names.txt. My script reads the first filename, and then pulls that file from a remote site.

After grabbing the file, the script processes it a bit before looping back to the top to read the next filename in names.txt.

If I comment out Section A (see code snippet) and manually assign one filename to $name, everything works fine. However, when leaving Section A in the script, it throws this error:

"Warning: fopen(http://www.exampleSite.com/name.csv) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 404"

If I put the URL into my browser, it works fine.

Completely baffled.

Any help please?

Code: Select all

// Section A ----- Reads all filenames from list
 
($nameHolder = fopen("names.txt", "r")) or die("Error opening names.txt");
while (!feof($nameHolder)){
$name = fgets($nameHolder);
 
 
//Grabs data from website
 
$url = 'http://www.exampleSite.com/'.$name.'.csv';
$handle = fopen("$url", "r");
 
/* Section B -----
 
   A 'while' loop follows that writes the contents of the remote csv to a local file,
   and then writes some of the data to a database.
 
   Now the script loops back to the top to grab the next file name in the names.txt list.
*/
 
Thanks!
AT

Re: Problems with fopen for remote file

Posted: Mon Jun 15, 2009 11:48 pm
by requinix
fgets will return the newline at the end of the line. So the URL it's trying to open, what's in $name, is actually

Code: Select all

http://www.exampleSite.com/name.csv
 
when you really wanted to give it

Code: Select all

http://www.exampleSite.com/name.csv
Note the subtle difference.

Run the result of that fgets through trim before assigning it to $name.

Re: Problems with fopen for remote file

Posted: Tue Jun 16, 2009 10:03 am
by atyler
Works great.

Thanks!

AT