文件名 feed1.xls 在 php 中不可读
我想使用 zend 框架解析一个 excel 文件.我去了 Zend Developer Zone 并找到了解决方案下载 phpexcelreader.我下载了代码,在 localhost 设置了一个项目并运行代码.当我尝试读取 .xlsx
文件时,它会生成错误
文件名 feed1.xlsx 不可读
我以 .xls
格式保存文件并运行代码成功解析文件.现在我想在我用 Zend framework
开发的项目中实现这个.我创建了一个模型,并在我的项目中 require_once
像这样在我的项目顶部编辑了 excelreader.
require_once 'Excelreader/Excel/reader.php';类 ExcelreaderModel 扩展 Zend_Db_Table{函数读取文件(){$data = new Spreadsheet_Excel_Reader();//设置输出编码.$data->setOutputEncoding('CP1251');//$data->read('Excelreader/Excel/feed1.xls');$data->read('feed1.xls');echo '';打印_r($数据);echo '</pre>';}}
我在我的控制器中调用了这个模型函数.但它产生了与我使用 .xlsx
文件在 localhost 上发现的相同的错误.但我正在阅读 .xls
文件,该文件由在 localhost 上的简单项目中运行的代码解析.我也在本地运行 zend 框架.
我的代码有什么问题?或者有什么办法可以做同样的任务.?
解决方案据我所知,您看到的错误是由这行代码设置的:
if(!is_readable($sFileName)) {$this->error = 1;返回假;}
由于某种原因,它无法读取您提供的文件,要么是权限/用户问题,要么是文件路径错误.
如果你在 *nix 服务器上,你应该确保运行网络服务器的用户有读取 excel 文件的权限.Web 服务器可能以与文件所有者不同的用户身份运行.您也可以尝试将权限设置为 666.
如果它只是找不到文件,提供完整路径可能会有所帮助,(例如 $data->read('/usr/local/apache2/htdocs/Excelreader/feed1.xls');
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_once
ed 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.?
解决方案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');
相关文章