为需要文件路径的 PHP 函数提供文件句柄
Provide a file handle to a PHP function expecting a file path
使用PHPExcel,我想从互联网上读取一个文件。 PHPExcel 库似乎只适合打开本地文件,而不适合 URLs。这是我尝试过的:
<?php
require __DIR__ . '/vendor/autoload.php';
$string = file_get_contents('http://opendatakit.org/wp-content/uploads/static/sample.xls');
$stream = fopen('php://memory','r+');
fwrite($stream, $string);
rewind($stream);
$objPHPExcel = PHPExcel_IOFactory::load('php://memory');
这是我收到的错误:
Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with
message 'Could not open php://memory for reading! File does not
exist.'
我也试过直接传递 URL (PHPExcel_IOFactory::load('http://opendatakit.org/wp-content/uploads/static/sample.xls')
)。类似错误。
Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with
message 'Could not open
http://opendatakit.org/wp-content/uploads/static/sample.xls for
reading! File does not exist.'
编辑:还尝试了一个临时文件
$string = file_get_contents('http://opendatakit.org/wp-content/uploads/static/sample.xls');
$temp = tmpfile();
fwrite($temp, $string);
fseek($temp, 0);
$objPHPExcel = PHPExcel_IOFactory::load($temp);
这次有不同的错误:
Warning: pathinfo() expects parameter 1 to be string, resource given
in /project/vendor/phpoffice/phpexcel/Classes/PHPExcel/IOFactory.php
on line 224
Warning: file_exists() expects parameter 1 to be a valid path,
resource given in
/project/vendor/phpoffice/phpexcel/Classes/PHPExcel/Reader/Excel2007.php
on line 81
Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with
message 'Could not open Resource id #10 for reading! File does not
exist.'
你的思路很好。但是 PHPExcel 需要一个文件路径才能正常工作。
您可以尝试使用此示例:
$string = file_get_contents('http://opendatakit.org/wp-content/uploads/static/sample.xls');
$tmp = tempnam(sys_get_temp_dir(), "FOO");
file_put_contents($tmp, $string);
$objPHPExcel = PHPExcel_IOFactory::load($tmp);
//Perform all your operations
// ...
unlink($tmp);
参见 PHP tempnam() 手册
使用PHPExcel,我想从互联网上读取一个文件。 PHPExcel 库似乎只适合打开本地文件,而不适合 URLs。这是我尝试过的:
<?php
require __DIR__ . '/vendor/autoload.php';
$string = file_get_contents('http://opendatakit.org/wp-content/uploads/static/sample.xls');
$stream = fopen('php://memory','r+');
fwrite($stream, $string);
rewind($stream);
$objPHPExcel = PHPExcel_IOFactory::load('php://memory');
这是我收到的错误:
Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open php://memory for reading! File does not exist.'
我也试过直接传递 URL (PHPExcel_IOFactory::load('http://opendatakit.org/wp-content/uploads/static/sample.xls')
)。类似错误。
Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open http://opendatakit.org/wp-content/uploads/static/sample.xls for reading! File does not exist.'
编辑:还尝试了一个临时文件
$string = file_get_contents('http://opendatakit.org/wp-content/uploads/static/sample.xls');
$temp = tmpfile();
fwrite($temp, $string);
fseek($temp, 0);
$objPHPExcel = PHPExcel_IOFactory::load($temp);
这次有不同的错误:
Warning: pathinfo() expects parameter 1 to be string, resource given in /project/vendor/phpoffice/phpexcel/Classes/PHPExcel/IOFactory.php on line 224
Warning: file_exists() expects parameter 1 to be a valid path, resource given in /project/vendor/phpoffice/phpexcel/Classes/PHPExcel/Reader/Excel2007.php on line 81
Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open Resource id #10 for reading! File does not exist.'
你的思路很好。但是 PHPExcel 需要一个文件路径才能正常工作。 您可以尝试使用此示例:
$string = file_get_contents('http://opendatakit.org/wp-content/uploads/static/sample.xls');
$tmp = tempnam(sys_get_temp_dir(), "FOO");
file_put_contents($tmp, $string);
$objPHPExcel = PHPExcel_IOFactory::load($tmp);
//Perform all your operations
// ...
unlink($tmp);
参见 PHP tempnam() 手册