Problems with fopen for remote file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
atyler
Forum Newbie
Posts: 17
Joined: Tue May 26, 2009 10:28 pm

Problems with fopen for remote file

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problems with fopen for remote file

Post 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.
atyler
Forum Newbie
Posts: 17
Joined: Tue May 26, 2009 10:28 pm

Re: Problems with fopen for remote file

Post by atyler »

Works great.

Thanks!

AT
Post Reply