2

I'm fairly new to this so I wanted to ask a question. I uploaded an excel file using html and when I try to learn how to read excel files using php, this is what I've got from IBM:

<?php

$filename = basename($_FILES["file"]["name"]);

$ext = substr($filename, strrpos($filename, '.') + 1);

function get_data($qnum, $qtype, $qname, $qtext, $atext, $pcredit, $rcounts, $r, $qcount, $cfac, $sd, $disc_inx, $disc_coef)
{
    global $data;

    $data[] = array('QNum' => $qnum, 'Qtype' => $qtype, 'Questionname' => $qname, 'Questiontext' => $qtext,
        'Answertext' => $atext, 'PartialCredit' => $pcredit, 'RCounts' => $r, 'QCount' => $qcount,
        'Correctionfacility' => $cfac, 'DiscriminationIndex' => $disc_inx, 'DiscriminationCoefficient' => $disc_coef);
}

if ($ext == "xls") {
    $dom = DOMDocument::load($_FILES['file']['tmp_name']);
    $rows = $dom->getElementsByTagName('Row');
    $first_row = true;
    foreach ($rows as $row) {
        if (!$first_row) {
            $qnum = "";
            $qtype = "";
            $qname = "";
            $qtext = "";
            $atext = "";
            $pcredit = "";
            $r = "";
            $qcount = "";
            $cfac = "";
            $disc_inx = "";
            $disc_coef = "";

            $index = 1;
            $cells = $row->getElementsByTagName('Cell');
            foreach ($cells as $cell) {
                $ind = $cell->getAttribute('Index');
                if ($ind != null)
                    $index = $ind;

                if ($index == 1)
                    $qnum = $cell->nodeValue;
                if ($index == 2)
                    $qtype = $cell->nodeValue;
                if ($index == 3)
                    $qname = $cell->nodeValue;
                if ($index == 4)
                    $qtext = $cell->nodeValue;
                if ($index == 5)
                    $atext = $cell->nodeValue;
                if ($index == 6)
                    $pcredit = $cell->nodeValue;
                if ($index == 7)
                    $r = $cell->nodeValue;
                if ($index == 8)
                    $qcount = $cell->nodeValue;
                if ($index == 9)
                    $cfac = $cell->nodeValue;
                if ($index == 10)
                    $disc_inx = $cell->nodeValue;
                if ($index == 11)
                    $disc_coef = $cell->nodeValue;

                $index += 1;
            }
            get_data($qnum, $qtype, $qname, $qtext, $atext, $pcredit, $r, $qcount, $cfac, $disc_inx, $disc_coef);
        }
        $first_row = false;
    }
}

else {
    echo "Invalid file!";
}
?>

And I got a syntax error

Warning: DOMDocument::load(): Start tag expected, '<' not found in /tmp/phpwUIpyZ, line: 1 in /var/www/moodle/question/upload_file.php on line 16 Fatal error: Call to a member function getElementsByTagName() on a non-object in /var/www/moodle/question/upload_file.php on line 17.

What is the error on my code? Need help please thanks!

2 Answers 2

5

Excel is not an valid DOMDocument , so of course you can't use DOMDocument for it :)

I would suggest using something ready such as PHPExcelReader.

Good Luck,
Shai.

Sign up to request clarification or add additional context in comments.

Comments

1

As the error message say, $dom is a non-object - in other words, DOMDocument::load returned something, but not an object. There could be various reasons for that, but the most likely ones are:

  • file doesn't exist or not readable
  • file is malformed (not a valid DOM document), parsing failed

See the manual: http://php.net/manual/en/domdocument.load.php

Note also that you seem to be trying to parse an XLS file as a DOM document - that won't fly, those are completely different file formats.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.