app/config.php 下配置了 'public_path' => base_path(false) . DIRECTORY_SEPARATOR . 'public',
上傳excel文件到public指定目錄下可以
但是讀取文件時(shí)候出錯(cuò)
$allFilePath = public_path('/storage/'.$filePath); //第一行是文件路徑
$allSheets = Excel::import($realPath)->toArray(); //這一行就報(bào)錯(cuò)了,在本地可以,一切正常
采用的讀取excel庫(kù)是 dcat/easy-excel 1.1版本
也按照網(wǎng)上的教程 function.php 里寫了一個(gè)函數(shù):
function get_public_resource_path($path)
{
static $resourcePathMap = [];
if (!\class_exists(\Phar::class, false) || !\Phar::running()) {
return $path;
}
$tmpPath = runtime_path() . DIRECTORY_SEPARATOR . 'temp';
if (!is_dir($tmpPath)) {
mkdir($tmpPath);
}
$filePath = $tmpPath . DIRECTORY_SEPARATOR . basename($path);
clearstatcache();
if (!isset($resourcePathMap[$path]) || !is_file($filePath)) {
file_put_contents($filePath, file_get_contents($path));
$resourcePathMap[$path] = $filePath;
}
return $resourcePathMap[$path];
}
在使用的時(shí)候
$allFilePath = public_path("/storage/". $filePath);
$realPath = get_public_resource_path($allFilePath);
$allSheets = Excel::import($realPath)->toArray();
依然報(bào)錯(cuò),請(qǐng)求大佬指點(diǎn)迷津!