Laravel中storage_path('/app/public/')的具体路径是什么?文件应存放于何处?
Laravel storage_path路径与文件存放问题解答
1. storage_path()对应的物理路径
Laravel框架中storage_path()辅助函数默认指向项目根目录下的storage文件夹。
你代码中调用的storage_path('/app/public/examResult/' . $this->argument('file')),拼接后的完整绝对路径为:
<你的Laravel项目根目录>/storage/app/public/examResult/<你传入的文件名>
2. 当前存放位置错误原因
你将文件放在laravelproject/app/public/examResult路径下,和代码预期读取的路径完全不匹配,这是触发Not found this file!报错的直接原因。
3. 解决方案
- 方案一:不修改代码,调整文件存放位置
把待导入的Excel文件移动到以下路径即可,若对应文件夹不存在请手动创建:
注意确保该目录及文件对命令行执行用户开放读权限。<你的Laravel项目根目录>/storage/app/public/examResult/
要是拿不准路径是否匹配,可以在文件判断逻辑前加一行代码打印系统实际查找的路径,运行命令后直接对比即可:dd(storage_path('/app/public/examResult/' . $this->argument('file'))); - 方案二:不修改文件存放位置,调整代码路径
如果你要保持文件存放在项目的public目录下(标准Laravel结构中public目录直接在项目根目录,不在app目录下,自定义结构可自行调整),可以把代码中的storage_path替换为public_path,修改后代码参考:public function handle() { $filePath = public_path('/examResult/' . $this->argument('file')); if ( ! File::exists($filePath) ) { dd('Not found this file!'); } Excel::import(new ImportsExamResultImport(), $filePath); dd('Import excel was successfully'); }
内容的提问来源于stack exchange,提问作者Pouya Vaghefi
相关产品推荐
相关产品推荐

