-2
<?php
include 'model.php';
$rs=new database();
if(isset($_POST["Import"])){

    echo $filename=$_FILES["file"]["tmp_name"];

        if($_FILES["file"]["size"] > 0)
        {

            $file = fopen($filename, "r");
            while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
            {

                $res=$rs->insert($emapData[0],$emapData[1],$emapData[2],$emapData[3],$emapData[4],$emapData[5]);
                $result=mysql_fetch_array($res);
                if(! $result )
                {
                    echo "<script type=\"text/javascript\">
                    alert(\"Invalid File:Please Upload CSV File.\");
                    window.location = \"result.php?msg=valid\"
                    </script>";
                }
            }
            fclose($file);
            echo "<script type=\"text/javascript\">
            alert(\"CSV File has been successfully Imported.\");
            window.location = \"result.php?msg=valid\"
            </script>";
            mysql_close($conn); 
        }
} 
?>

this code only uploads csv file but i want to upload xls too with this code. if possible i want to upload all format of excel . and the rest of code is working fine and also i dont want to change the method.

mapmalith
  • 1,303
  • 21
  • 38
Yogesh Arya
  • 134
  • 2
  • 14

1 Answers1

0

Download PHPExcel https://github.com/PHPOffice/PHPExcel

and create this function

function getDataFromExcel($filename)
{
    $excel      = PHPExcel_IOFactory::load($filename);

    $sheet      = $excel->getSheet(0);
    $highestRow = $sheet->getHighestRow();
    $sheetData  = $sheet->toArray(null, true, true, true);

    return $sheetData;

}

It will return data in array

if you want to know the type of file use this method

function getFileType($key)
{
    //Define type
    $type = 'unknow';
    if(isset($_FILES[$key])) {
        $file     = $_FILES[$key];
        $fileType = $file['type'];
        if (strrpos($fileType, 'csv')) {
            $type = 'csv';
        } else if (($fileType == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') || ($fileType == 'application/vnd.ms-excel')) {
            $type = 'excel';
        }
    }
    return $type;
}