 |
|
|
|
  |
|
Super Member
|
|
Join Date: Aug 2003
Location: Costa Del Sheffield
Posts: 2,708
|
Thanks: 0
Thanked 1 Time in 1 Post
|
|
php fopen / fgetcsv
Hi,
Okay the words simplest script has gone wrong and i cant for the life of me work out why
I have a script, finds a datafeed url, checks it got something in it, imports the file into a db. Easy.
PHP Code:
// connect to db, set $boredofthehighstreet URL etc
$Checkboredofthehighstreet = file_get_contents($boredofthehighstreet);
if (strlen($Checkboredofthehighstreet) >= 200) {
$row = 1;
$handle = fopen("$boredofthehighstreet","r");
while ($data = fgetcsv($handle, 100000, ",")) {
$row++;
$sql = "INSERT INTO table_name (...)
VALUES (...)";
$results = mysql_query($sql);
fclose ($handle); }
}
Works fine on one site, then on the other all I get is
Warning: fgetcsv(): 4 is not a valid stream resource in /home/site/public_html/cron/importcsv.php on line 20
line 20 is:
while ($data = fgetcsv($handle, 100000, ",")) {
it feels like the easiest thing in the world to fix but its done me 
__________________
Dan Morley
Alpharooms.com
daniel at alpharooms dot com - Hotels, Flights, Airport Transfers, Care Hire + More! sign up
My Blog | Cheap Holidays
|
|
|
|
  |
|
Registered User
|
|
Join Date: Aug 2007
Posts: 87
|
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Re: php fopen / fgetcsv
Hi Dan
I do not know if this is any use but I hit a similar issue a few months ago, before resorting to my trusty Magic Parser instead of my own code. From discussions on php.net:
"Only problem with fgetcsv(), at least in PHP 4.x -- any stray slash in the data that happens to come before a double-quote delimiter will break it -- ie, cause the field delimiter to be escaped. I can't find a direct way to deal with it, since fgetcsv() doesn't give you a chance to manipulate the line before it reads it and parses it...I've had to change all occurrences of '\"' to '" in the file first before feeding ot to fgetcsv(). Otherwise this is perfect for that Microsoft-CSV formula, deals gracefully with all the issues."
"This function appears to assume that \" is an escaped quote - similar to "" - which may lead to incorrect results while reading some files. Found while running under PHP 5.1.6."
I hope this helps.
Last edited by confuscius; 04-06-08 at 08:13 PM.
Reason: Typos
|
|
|
|
  |
|
ad9couk
|
|
Join Date: Dec 2003
Location: Chippenham Wiltshire
Posts: 408
|
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Re: php fopen / fgetcsv
Just a thought (probably wrong) do you have the error levels configured differently by php , i think phpinfo displays the error settings , - (as this is only a warning) -if so try suppressing warnings
|
|
|
|
  |
|
Super Member
|
|
Join Date: Aug 2003
Location: Costa Del Sheffield
Posts: 2,708
|
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Re: php fopen / fgetcsv
well, cheers guys but no luck.
cant find anything odd in the CSV, from POR so id expect it to be spot on  Works fine with another site on the same server, cant get my head around it
toneharb - again, same server which is whats confusing the hell out of me. Checked db and nothing being inserted so it seems to be a bit of a problem
urg... 
__________________
Dan Morley
Alpharooms.com
daniel at alpharooms dot com - Hotels, Flights, Airport Transfers, Care Hire + More! sign up
My Blog | Cheap Holidays
|
|
|
|
  |
|
ad9couk
|
|
Join Date: Dec 2003
Location: Chippenham Wiltshire
Posts: 408
|
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Re: php fopen / fgetcsv
I would try ...
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
... Just incase
|
|
|
|
  |
|
Registered User
|
|
Join Date: Aug 2007
Posts: 87
|
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Re: php fopen / fgetcsv
Could you echo each line to the screen and see where and if it falls over?
Only other thing like this that I have hit was where addition line returns were embedded that threw things off and not visible on notepad! in effect random new records were being created that did not match the schema.
|
|
|
|
  |
|
Super Member
|
|
Join Date: Aug 2003
Location: Costa Del Sheffield
Posts: 2,708
|
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Re: php fopen / fgetcsv
hi toneharb - tried that, same error?
echo / print_r gives Resource id #4?
__________________
Dan Morley
Alpharooms.com
daniel at alpharooms dot com - Hotels, Flights, Airport Transfers, Care Hire + More! sign up
My Blog | Cheap Holidays
|
|
|
|
  |
|
Registered User
|
|
Join Date: Sep 2007
Posts: 359
|
Thanks: 1
Thanked 1 Time in 1 Post
|
|
Re: php fopen / fgetcsv
as confuscius says , check additional line returns, this has happened to me in the past , its usually the simplist things 
|
|
|
|
  |
|
Super Member
|
|
Join Date: Aug 2003
Location: Costa Del Sheffield
Posts: 2,708
|
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Re: php fopen / fgetcsv
this is driving me mad, the file im trying to open is from POR, i use the same script with the same file to insert it into another database on a different site on the same server
i move the script and cut out some of the crap, fetching the same file from POR and inserting it into a new db and it doesnt work?
I keep adding bits in to try and figure out whats going wrong and just have a screen full of errors!
anyone want to rent skirts.co.uk for the next 12 months till I figure this out?
dan... hair. out. pulling.
__________________
Dan Morley
Alpharooms.com
daniel at alpharooms dot com - Hotels, Flights, Airport Transfers, Care Hire + More! sign up
My Blog | Cheap Holidays
|
|
|
|
  |
|
Registered User
|
|
Join Date: Aug 2007
Posts: 87
|
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Re: php fopen / fgetcsv
You could also try loading the file in excel / open office and see if you get any column misalignments from the data - it may highlight this issue.
Otherwise, is it possible to zip the offending file and let some of us have a dig?
|
|
|
|
  |
|
Super Member
|
|
Join Date: Aug 2003
Location: Costa Del Sheffield
Posts: 2,708
|
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Re: php fopen / fgetcsv
hi confuscius, cheers for your help with this, offending file URL is below, its a POR server so ill strip the link out in about 15 mins or so
[url gone, hope someone got it and can help]
fairly standard (and cant see how the problem is with the file as it works with another site)
full php script is basically as above, simplest thing in the world. php 4.4 but again, same php 4.4 version that my other site runs using the same script & feed!

__________________
Dan Morley
Alpharooms.com
daniel at alpharooms dot com - Hotels, Flights, Airport Transfers, Care Hire + More! sign up
My Blog | Cheap Holidays
|
| |