Reputation: 211
I currently have the block of code below processing a form where the user uploads a file. How do I get this to return an error if the user uploads a file that fgetcsv cannot process? Right now, when I try to have it process an image my script continues to run and I never receive an error message.
I tried testing $data to determine if it was null but all I got were errors.
if (($handle = fopen($_FILES['fileUpload']['tmp_name'], 'r')) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$total = count($data);
for ($e=0; $e < $total; $e++) {
echo $data[$e] . "<br />";
}
echo "<br />";
}
fclose($handle);
}
As an FYI - I am only printing out the csv contents for testing purposes. In the final script I will just save the array in a session.
UPDATED: As @hakre suggested, I asked the wrong question. I have since taken his answer and since I was having trouble with it and I did not completely understand it, I modified it be what you see below.
Everything is working great except for that the line This was fixed based on @harke comment. if ($row === null) continue;
does not work. I have tried it with and without brackets around null
. It does not seem to matter whether the empty line is in the middle of the file or if it's the last line.if ($row === null) continue;
was replaced with if ($row === array(null)) continue;
$uploadTempfile = $_FILES['fileUpload']['tmp_name'];
$file = new SplFileObject($uploadTempfile);
$file->setFlags(SplFileObject::READ_CSV);
$limit = new LimitIterator($file, 0, 10); // scan the first 10 lines only
foreach ($limit as $row) {
if ($row === array(null)) continue; // skip empty lines
$columnCount[] = count($row);
}
if(count(array_unique($columnCount)) === 1){
echo 'file is valid';
} else {
echo 'file is INVALID';
}
I have updated the above block of code based on the comments and answers below so that it now works correctly.
Upvotes: 2
Views: 3586
Reputation: 198209
How do I get this to return an error if the user uploads a file that fgetcsv cannot process?
That is probably the wrong question. fgetcsv
can process any file. So as that is always true, such a function would return always success and never an error.
What you probably mean is that you would like to inspect the CSV file and somewhat verify it's contents if it is likely to be such kind of a CSV file you're looking for.
This would also require that you make clear if the file should actually be of some specific nature, e.g. always the same number of columns, which might be what you're looking for.
The following code-example checks the first 10 lines of a CSV file that it at least contains a single line, each of those 10 lines (if not empty) has the exact same count and the count is larger than one:
$uploadTempfile = $_FILES['fileUpload']['tmp_name'];
$file = new SplFileObject($uploadTempfile);
$file->setFlags(SplFileObject::READ_CSV);
$limit = new LimitIterator($file, 0, 10); // scan the first 10 lines only
foreach ($limit as $row) {
if ($row === [null]) continue; // skip empty lines
$rowCount = count($row);
if (!isset($count)) {
$count = $rowCount;
}
if ($rowCount !== $count) {
break;
}
}
$valid = (isset($count) and $rowCount === $count and $count > 1);
Upvotes: 1