Laravel,正在上传文件';s网址
我目前正在从事一个Laravel 5.5项目,我希望上传文件,和,然后,我想获取文件的url(我必须使用它的客户端) 现在,我的代码如下所示:Laravel,正在上传文件';s网址,laravel,url,laravel-5,laravel-5.5,Laravel,Url,Laravel 5,Laravel 5.5,我目前正在从事一个Laravel 5.5项目,我希望上传文件,和,然后,我想获取文件的url(我必须使用它的客户端) 现在,我的代码如下所示: public function pdfUploader(Request $request) { Log::debug('pdfUploader is called. ' . $request); if ($request->hasFile('file') && $request->file('file')->is
public function pdfUploader(Request $request)
{
Log::debug('pdfUploader is called. ' . $request);
if ($request->hasFile('file') && $request->file('file')->isValid()) {
$extension = $request->file->extension();
$fileName = 'tmp' . round(microtime(true) * 1000) . '.' . $extension;
$path = $request->file->storeAs('temp', $fileName);
return ['status' => 'OK', 'path' => URL::asset($path)];
}
return ['status' => 'NOT_SAVED'];
}
它工作正常,我恢复了OK状态和路径,但是当我想使用路径时,我得到了HTTP 404。我检查过了,文件上传得很好
我的想法是,我应该在路由中注册新的url。如果我必须这样做,我如何动态地执行它,如果没有必要,我的函数有什么问题
提前回答。默认情况下,laravel将所有上传的文件存储到存储目录中,例如,如果您调用
$request->file->storeAs('temp',file.txt')
laravel将在storage/app/
中创建temp
文件夹,并将您的文件放在那里:
$request->file->storeAs('temp', 'file.txt'); => storage/app/temp/file.txt
$request->file->storeAs('public', 'file.txt'); => storage/app/public/file.txt
但是,如果您想从web访问上载的文件,有两种方法:
将上载的文件移动到公共目录中
$request->file->move(public_path('temp'), $fileName); // => public/temp/file.txt
URL::asset('temp/'.$fileName); // http://example.com/temp/file.txt
php artisan storage:link
注意:确保您的web服务器具有写入公共目录的权限
创建从存储目录到公共目录的符号链接
$request->file->move(public_path('temp'), $fileName); // => public/temp/file.txt
URL::asset('temp/'.$fileName); // http://example.com/temp/file.txt
php artisan storage:link
此命令将创建一个从public/storage
到storage/app/public
的符号链接,在这种情况下,我们可以将文件存储到storage/app/public
中,并通过符号链接从web访问它们:
$request->file->storeAs('public', $fileName); // => storage/app/public/file.txt
URL::asset('storage/'.$fileName); // => http://example.com/stoage/file.txt
如果你想用作资产,一定要在公共场所储蓄。我有一个用于导入的文件夹$文件->移动(基本路径()。/public/import/,“uploadedfile”);这条路看起来像什么?我想您可能已将其上载到了不正确的位置。下面的答案显示了问题所在,我已将文件上载到存储/app/temp,并且必须将其移动到公共应用程序。:)现在官方文件建议创建一个Ohh,感谢你给出了这么长的答案。有你的向导,我可以做到!我不得不走到公共道路上,然后做了一个象征性的链接,它起作用了。又来了!