PHP OOP文件处理程序无法获取数据

时间:2014-11-28 16:14:45

标签: php oop

我正在构建一个文件上传器系统并清理我的代码我正在深入研究OOP PHP。这是我的代码

<?php
    //class fileData manipulates files and pulls thier data
    class fileData{

        //global file variable
        protected $file = null;
        //global file handler
        protected $handle = null;
        //global file data object array
        protected $fileData = array();

        //default constructor to initialize the class and return the file object
        public function __construct($file){
            $this->file = $file;
            $this->openFile();
        }

        //open the file and return the handler. returns resource if successful, false if failed
        private function openFile(){
                $this->handle = fopen($this->$file, 'r');
        }


        //get the data from the file and set it as an object
        public function getFileData(){
            //check the file handler
            if(!$this->handle)
                exit("Unable to open the file"); //if the file does not open exit the script 


            $headers = array(); //set the column titles in an array for the objecy attribute name
            $row = 1; //set the row count
            while(($data = fgetcsv($this->handle, 0, ',')) !== false){
                //if it is the first row get the column titles
                if($row == 1){
                    for($i = 0; count($data); $i++){
                        //object attribute names array
                        array_push($headers, $data[$i]);
                    }
                }else{
                    //build the object array
                    $object = array();
                    for($j = 0; count($headers); $j++){
                        $object[$headers[$j]] = $data[$j];
                    }
                    array_push($this->fileData, $object);
                }
                $row++;
            }
            //return the object array
            return $this->fileData;
        }
    }
?>

然后在html方面我有以下代码

<?php
require_once 'assets/server/upload/fileData.class.php';
if($_POST){
        if(isset($_FILES)){
            $fileData = new fileData($_FILES['spreadsheet']['tmp_name']);
            print_r($fileData->getFileData());
        }
    }
?>
<html>
    <form action="" class="navbar-form navbar-left" method="post" enctype='multipart/form-data'>
        <div class="form-group">
            <input type='file' class='form-control' name='spreadsheet'>
        </div>                      
        <input type='submit' class="btn btn-success" value='Upload file'>
    </form>
</html>

我有print_r函数只是为了测试以确保所有内容都正确打印出来,但是在表单提交后我没有收到任何数据。有关为什么会发生这种情况以及如何解决问题的想法?

2 个答案:

答案 0 :(得分:2)

在你的句子中

while ($data = fgetcsv($this->handle, 0, ',') !== false)

您正在为$data分配一个普通布尔值。你没有得到你期望的数组。应该是:

while (($data = fgetcsv($this->handle, 0, ',')) !== false)

答案 1 :(得分:1)

在与社区的一些成员进行了愉快的合作之后,这是我们提出的最终结果

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    //class fileData manipulates files and pulls thier data
    class fileData{
        //global file variable
        protected $file = null;
        //global file handler
        protected $handle = null;
        //global file data object array
        protected $fileData = array();

        //default constructor to initialize the class and return the file object
        public function __construct($file){
            $this->file = $file;
            $this->openFile();
        }

        //open the file and return the handler. returns resource if successful, false if failed
        private function openFile(){
                $this->handle = fopen($this->file, 'r'); //corrected the syntax thank you -Fred -ii-
        }


        //get the data from the file and set it as an object
        public function getFileData(){
            //check the file handler
            if(!$this->handle)
                exit("Unable to open the file"); //if the file does not open exit the script 
            $headers = array(); //set the column titles in an array for the objecy attribute name
            $row = 1; //set the row count
            //make sure data is being set to this proper array, thank you clapas for your input
            while(($data = fgetcsv($this->handle, 0, ',')) !== false){
                //if it is the first row get the column titles
                if($data[0] === "---")
                    break;
                if($row == 1){
                    for($i = 0; $i < count($data); $i++){
                        //object attribute names array
                        array_push($headers, $data[$i]);
                    }
                }else{
                    //build the object array
                    $object = array();
                    for($j = 0; $j < count($data); $j++){
                        $object[$headers[$j]] = $data[$j];
                    }
                    array_push($this->fileData, $object);
                }
                $row++;
            }
            //return the object array
            return $this->fileData;
        }
    }
?>