为什么 PhpSpreadsheet 无法在 public:// 中识别我的 Xlsx 文件?
Why does PhpSpreadsheet fail to identify my Xlsx file in public://?
我正在开发一个导入 Xlsx 文件并将行插入数据库的 Drupal 模块。一切正常,除了当我上传的文件在 public
中时 PhpSpreadsheet 抛出错误 "PHP message: Uncaught PHP Exception InvalidArgumentException: "File "" does not exist." at File.php line 137"
。当文件位于 base_path()
中时,不会发生此错误。第 137 行抛出异常,说它不是文件,而是 \dpm(is_file($uri))
returns TRUE
。为什么会这样?下面是我的代码:
public function submitForm(array &$form, FormStateInterface $form_state) {
$connection = \Drupal::database();
$fid = $form_state->getValue("xlsx_upload");
$input = File::load(reset($fid));
$input->setPermanent();
$uri = \Drupal::service('file_system')->realpath($input->getFileUri());
//$uri = '/home/joe/file.xslx'; <--- this works perfectly.
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($uri);
$spreadsheet = $reader->load($filename);
$dataArray = $spreadsheet->getActiveSheet()->toArray();
$connection->truncate("mitacan")->execute();
$connection->query("SET NAMES utf8");
foreach ($dataArray as $dataItem) {
$connection->insert('mitacan')
->fields([
'stokkodu' => $dataItem[0],
'logo' => $dataItem[1],
'resim1' => $dataItem[2],
'grupkodu' => $dataItem[3],
'grupadi' => $dataItem[4],
'grupindex' => $dataItem[5],
'paketadedi' => $dataItem[6],
])
->execute();
}
drupal_set_message('Excel verisi veritabanına eklendi.');
}
$spreadsheet = $reader->load($filename);
$文件名未定义。请改用 $uri。
我正在开发一个导入 Xlsx 文件并将行插入数据库的 Drupal 模块。一切正常,除了当我上传的文件在 public
中时 PhpSpreadsheet 抛出错误 "PHP message: Uncaught PHP Exception InvalidArgumentException: "File "" does not exist." at File.php line 137"
。当文件位于 base_path()
中时,不会发生此错误。第 137 行抛出异常,说它不是文件,而是 \dpm(is_file($uri))
returns TRUE
。为什么会这样?下面是我的代码:
public function submitForm(array &$form, FormStateInterface $form_state) {
$connection = \Drupal::database();
$fid = $form_state->getValue("xlsx_upload");
$input = File::load(reset($fid));
$input->setPermanent();
$uri = \Drupal::service('file_system')->realpath($input->getFileUri());
//$uri = '/home/joe/file.xslx'; <--- this works perfectly.
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($uri);
$spreadsheet = $reader->load($filename);
$dataArray = $spreadsheet->getActiveSheet()->toArray();
$connection->truncate("mitacan")->execute();
$connection->query("SET NAMES utf8");
foreach ($dataArray as $dataItem) {
$connection->insert('mitacan')
->fields([
'stokkodu' => $dataItem[0],
'logo' => $dataItem[1],
'resim1' => $dataItem[2],
'grupkodu' => $dataItem[3],
'grupadi' => $dataItem[4],
'grupindex' => $dataItem[5],
'paketadedi' => $dataItem[6],
])
->execute();
}
drupal_set_message('Excel verisi veritabanına eklendi.');
}
$spreadsheet = $reader->load($filename);
$文件名未定义。请改用 $uri。