473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

fgetcsv returns false in dynamic read vs. hard-coded read ??

I am working with directories in PHP for the first time. I have code
that I've changed multiple times to try different things. I would think
this is pretty standard fare so I'm not sure why I can't seem to get it
right.

What I would like to see happen:

The code opens the directory and loops through the files, opening them
and processing them.

What is happening:

If I hard code the name of one particular file (it is always the same
file) in my test setting, fgetcsv does not return false and the code
runs fine. The file is parsed and all is well. However, if I let the
code open a directory and then loop through the files, this one
particular txt file does not work. For debug purposes, I have it
displaying each file name and it can display the name, it just won't
process it.

I've even tried setting a for loop and hard coding each file name
(since right now I know the names - I won't in the future) and the file
is processed. It is only when it is set to my $file var dynamically
rather than being hard coded.

Here is the code:
$handle_dir = opendir('../directory_name');
/* loop over the directory. */
$countfile=0;
while (false !== ($file = readdir($handle_dir))) {
$countfile++;
$filevalues = "";
//Skip the first two files that where found because they are
"." and ".."
if ($countfile 2) {
//echo $file;
//begin loop through each file name
$handle = fopen($file, "r");
echo "Filename is: $file<BR>";//at this point the
code can print the file name
if (false == ($filevalues =
fgetcsv($handle,6021,','))){
echo 'problems<br>';//for one particular file,
this always displays
}
while (false !== ($filevalues =
fgetcsv($handle,6021,','))){
echo 'I hit the loop: '.$file.'<br />';
//processing code here
}

}

}

closedir($handle_dir);

here is the hard-coded version:
for ($i=0;$i < 2; $i++){
switch ($i) {
case 0:
$file = 'filename.csv';
break;
case 1:
$file = 'filename2.csv';
break;
default:
die('Out of loop');
break;
}
$handle = fopen($file, "r");
echo "Filename is: $file<BR>";//at this point the
code can print the file name
if (false == ($filevalues =
fgetcsv($handle,6021,','))){
echo 'problems<br>';//for one particular file,
this always displays
}
while (false !== ($filevalues =
fgetcsv($handle,6021,','))){
echo 'I hit the loop: '.$file.'<br />';
//processing code here
}

}

closedir($handle_dir);

I appreciate any help.

Sep 20 '06 #1
0 1589

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lkrubner | last post by:
www.php.net says: >>>>>>>>>>>> Only for SELECT,SHOW,EXPLAIN or DESCRIBE statements mysql_query() returns a resource identifier or FALSE if the query was not executed correctly. For other type of...
6
by: dan glenn | last post by:
hi. I'm having a problem using javasript to pass the value of a textarea (in a form) to a PHP script file. I want to code a 'preview' function into a guestbook entry page, using an HTML link...
59
by: Pierre Quentel | last post by:
Hi all, In some program I was testing if a variable was a boolean, with this test : if v in My script didn't work in some cases and I eventually found that for v = 0 the test returned True ...
2
by: Stephen Preston | last post by:
I have an xls worksheet I wish to export as a text or csv file to import with fgetcsv() The 'save as' function on excell lets me save as a comma separated value or tab delimited text files....
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
1
by: Jonaed | last post by:
This is my code: $handle = fopen ($file, "r"); $c = 1; while (($values_list = fgetcsv ($handle, 1000, ',', '"')) !== FALSE) { echo $c . "<br>\n"; $c = $c +...
0
by: et | last post by:
fgetcsv() do unwanted "left trim" on fields started with locale characters (code page win-1250). Problem persist only if local character is first character in field for example, let say that C...
12
by: ross.oneill | last post by:
Hi, Is there any function in php that will match a word exactly and if it finds it, it returns true. For example if I search for "CA" strVar = "Bob is from Los Angeles CA" - return true ...
3
by: jrrdnx | last post by:
I have a form set up to allow someone to upload a .csv and populate its data into a database using the fgetcsv() function. All of the code works fine because everything in the .csv file is populated...
2
code green
by: code green | last post by:
I am trying to skip empty rows in a csv file The manual states But none of these tests seem to work. Anybody know why while(($data = fgetcsv($this->handle)) !== false) { ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.