Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9068439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:11:35+00:00 2026-06-16T17:11:35+00:00

I currently have the block of code below processing a form where the user

  • 0

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 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. This was fixed based on @harke comment. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:11:39+00:00Added an answer on June 16, 2026 at 5:11 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently have a media player that is streaming an mp3 file. When that
I have a Windows form which hosts a custom User Control. This User Control
I currently have an application that contains user statuses and support for comments on
I'm currently getting to grips with mod_rewrite and have ran into a stumbling block
I'm writing a simple shopping cart application and have hit a road block. Currently
How can I block user interaction with the my current location pin? I have
Currently have a drop down menu that is activated on a hover (from display:none
I currently have a XSLT 2.0 Stylesheet that I am trying to remove empty
I currently have one project that currently contains multiple packages. These packages make up
[SOLVED]: Applying proper list iteration procedure fixed problem. (Shown below) I currently have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.