phpexcel移植到sae上的一个致命bug的解决的办法

成绩查询分析->通过这个程序我悟出了一个道理:

很多功能仅用mysql语句就可以实现,远比用php等后台语言实现起来要快的多。这是优化程序的技巧之一。

然后我遇到了一个问题:

由于需要导入xls文件到数据库,而且要实现傻瓜式操作:

所以采用了phpexcel,但是上传到sae不好使

明明用sae的saestorage函数可以找到文件,但是phpexcel就是报错

=====

Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open http://score17-score.stor.sinaapp.com/score.xls for reading! File does not exist.' in /data1/www/htdocs/713/score17/1/Classes/PHPExcel/Reader/Abstract.php:196 Stack trace: #0 /data1/www/htdocs/713/score17/1/Classes/PHPExcel/Reader/Excel2003XML.php(96): PHPExcel_Reader_Abstract->_openFile('http://score17-...') #1 /data1/www/htdocs/713/score17/1/xls2.php(25): PHPExcel_Reader_Excel2003XML->canRead('http://score17-...') #2 {main} thrown in Classes/PHPExcel/Reader/Abstract.php on line 196

=====

网上没有答案,肿么办?

于是自己找,按照代码一个个分析,Excel2003XML.php继承自Abstract.php好吧分析吧

那个函数出问题了?

_openFile,加下划线很了不起啊,一看就是私有函数,直接找它老爹Abstract.php

找到了:

    /**
     * Open file for reading
     *
     * @param string $pFilename
     * @throws    PHPExcel_Reader_Exception
     * @return resource
     * 关键在于这里如何打开文件采用curl函数进行改进
     */
    protected function _openFile($pFilename)
    {
        // Check if file exists
        if (!file_exists($pFilename) || !is_readable($pFilename)) {
            throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
        }

        // Open file
        $this->_fileHandle = fopen($pFilename, 'r');
        if ($this->_fileHandle === FALSE) {
            throw new PHPExcel_Reader_Exception("Could not open file " . $pFilename . " for reading.");
        }
    }

问题感觉肯定出在这里,这里读文件的方法是本地io函数,而sae是不支持本地io的,所以出来了。

答案呼之欲出:看,Open file 竟然用fopen,当然不对sae的胃口了,哈哈,让我找到答案了,

=====

定义和用法

fopen() 函数打开文件或者 URL。

如果打开失败,本函数返回 FALSE。

语法

fopen(filename,mode,include_path,context)
参数描述
filename 必需。规定要打开的文件或 URL。
mode 必需。规定要求到该文件/流的访问类型。可能的值见下表。
include_path 可选。如果也需要在 include_path 中检索文件的话,可以将该参数设为 1 或 TRUE。
context 可选。规定文件句柄的环境。Context 是可以修改流的行为的一套选项。

=======

看,这就是这个函数的本质

利用sae的saestorage函数进行读取,认真分析,对phpexcel涉及到读取的内容进行修改就好了。

由于本人只需要用到sae的一小点内容,所以只改自己用到的就好。

明天再找sae中的替代函数,to be continued

 问题又来了

echo fopen('http://score17-score.stor.sinaapp.com/score.xls','r');

为什么直接读这个就可以呢?一样的道理啊,为什么这么直接读就能够返回资源呢?

同样是sae环境下啊?在phpexcel的类里面和直接有什么区别呢?

额---------------->自习观察,错误原来不是在fileopen,而是在

 // Check if file exists
        if (!file_exists($pFilename) || !is_readable($pFilename)) {
            throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
        }

 而是在这里啊,找不到文件的存在,但是能读,好稀奇啊!看来是file_exists的问题,

虽然我没有解决phpexcel的兼容问题,但是我最终解决了这个问题,那就是我不保存excel文件到saestorage上,在表单提交了文件之后,立即访问临时文件,然后读文件到数据库。然后对每次上传的文件加以记录。即可。

原文地址:https://www.cnblogs.com/ilangxm/p/3418735.html