使用PHPExcel,我想從互聯網上讀取文件。 PHPExcel庫似乎只適用於打開本地文件,而不是URL。下面是我的嘗試:提供PHP函數的文件句柄,期望文件路徑
<?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.'
是您的php.ini中設置允許打開外部URL? – Yolo