The filename feed1.xls is not readable in php

2020-04-05 09:24发布

问题:

Hi I want to parse an excel file using zend framework. I went to Zend Developer Zone and found a solution to download phpexcelreader. I downloaded the code set a project at localhost and run the code. When I treid to read an .xlsx file it generates an error

The filename feed1.xlsx is not readable

I saved the file in .xls format and run the code parsed the file successfully. Now I want to implement this in my project developed in Zend framework. I created a model, and in my project and require_onceed the excelreader at top of my project like this.

 require_once 'Excelreader/Excel/reader.php';
class ExcelreaderModel extends Zend_Db_Table
{
function readFile()
{
    $data = new Spreadsheet_Excel_Reader();

    // Set output Encoding.
    $data->setOutputEncoding('CP1251');

    //$data->read('Excelreader/Excel/feed1.xls');
    $data->read('feed1.xls');
    echo '<pre>';
    print_r($data);
    echo '</pre>';
}
}

I called this model function in my controller. But it is generating the same error which I found on localhost using .xlsx files. But I am reading .xls file which is parsed by the code running at simple project on localhost.I am also running zend framework at local.

What is wrong in my code? Or is there any way to do the same task.?

回答1:

As best I can tell, the error you are seeing is set by this line of code:

if(!is_readable($sFileName)) {
    $this->error = 1;
    return false;
}

For one reason or another, it cannot read the file you are giving, either because there is a permissions/user issue, or the path to the file is wrong.

If you are on a *nix server, you should make sure the user running the webserver has permission to read the excel file. The web server probably is running as a different user than who owns the file. You can also try setting the permissions to 666.

If it just ins't finding the file, providing a full path may help, (e.g. $data->read('/usr/local/apache2/htdocs/Excelreader/feed1.xls');



回答2:

I think problem is in encoding type of .xlxs file rather in setting file permission. If you change the permission it will not work for you. Problem is in OLE_IDENTIFIER (��ࡱ�).

So your file data must start with ��ࡱ� data. Even I am looking for proper solution for this. Can any one help ??



回答3:

The error could certainly be more descriptive. I added in a wrapper inside read() function like this:

if (file_exists($sFileName))
{
     ...
}else{
    die('The file ' . $sFileName . ' was not found');
}

And sure enough, I was looking in the wrong directory the whole time!!



回答4:

mb_internal_encoding("8bit"); in the head of the php file did the trick for me.



回答5:

Please convert your xlsx file to xls and it will work :)

it is working for me, you can try it.