文件名 feed1.xls 在 php 中不可读

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8983298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:55:19  来源:igfitidea点击:

The filename feed1.xls is not readable in php

phpexcelzend-framework

提问by Awais Qarni

Hi I want to parse an excel file using zend framework. I went to Zend Developer Zoneand 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 .xlsxfile it generates an error

嗨,我想使用 zend 框架解析一个 excel 文件。我去了Zend Developer Zone,找到了下载phpexcelreader的解决方案。我下载了代码,在 localhost 设置了一个项目并运行代码。当我尝试读取.xlsx文件时,它会生成错误

The filename feed1.xlsx is not readable

I saved the file in .xlsformat 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.

我以.xls格式保存文件并运行代码成功解析文件。现在我想在我开发的项目中实现这一点Zend framework。我创建了一个模型,并在require_once我的项目中像这样在我的项目顶部编辑了 excelreader。

 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 .xlsxfiles. But I am reading .xlsfile which is parsed by the code running at simple project on localhost.I am also running zend framework at local.

我在我的控制器中调用了这个模型函数。但它产生了与我使用.xlsx文件在 localhost 上发现的相同的错误。但是我正在读取.xls由在 localhost 上的简单项目中运行的代码解析的文件。我也在本地运行 zend 框架。

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

我的代码有什么问题?或者有什么办法可以做同样的任务。?

采纳答案by drew010

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.

如果你在 *nix 服务器上,你应该确保运行 web 服务器的用户有读取 excel 文件的权限。Web 服务器可能以与文件所有者不同的用户身份运行。您也可以尝试将权限设置为 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');

如果它只是找不到文件,则提供完整路径可能会有所帮助(例如 $data->read('/usr/local/apache2/htdocs/Excelreader/feed1.xls');

回答by kumar soneta

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

我认为问题在于.xlxs文件的编码类型而不是设置文件权限。如果您更改权限,它将对您不起作用。问题在OLE_IDENTIFIER (????).

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

所以你的文件数据必须以????数据开头。甚至我也在为此寻找合适的解决方案。有人可以帮忙吗??

回答by Enigmax

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

该错误当然可以更具描述性。我在 read() 函数中添加了一个包装器,如下所示:

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!!

果然,我一直在寻找错误的目录!!

回答by Mansur Fattakhov

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

mb_internal_encoding("8bit");在 php 文件的头部为我做了诀窍。

回答by Vishal P Gothi

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

请将您的 xlsx 文件转换为 xls,它会起作用:)

it is working for me, you can try it.

它对我有用,你可以试试。