【问题标题】:Laravel can not found a stored file 404 not NOT FOUNDLaravel 找不到存储的文件 404 not NOT FOUND
【发布时间】:2021-08-10 08:28:24
【问题描述】:

我只是一个初学者,我需要你的帮助。 编辑:即使我搜索 http://127.0.0.1:8000/storage/uploaded/the_filename 我得到 404 no found

我已经用过 php artisan storage:link 当我使用 <iframe src="/storage/uploaded/{{$data->doc}}"> 时出现错误 404 Not Found

即使我写了<iframe src="/storage/uploaded/1628544440.docx">,我仍然得到404 Not Found1628544440.docx 是 DB 和 uploaded 文件夹中的文件名。

文件夹/storage/app/public/uploaded中的文档与数据库中的documents->doc同名。

我可以在代码中更改什么?

DocumentController:

public function store(Request $request)
{
    $request->validate([
        'doc' =>  'required',...
    ]);

    $document = new document();

    $file = $request->file('doc');
    $filename = time().'.'.$file->getClientOriginalExtension() ;
    $file->storeAs('uploaded', $filename);

    $document->doc = $request->input('doc', $filename);
    $document->candidate_id = $candidate_id; 
    $document->save();
}

public function show($id)
{
    // to select a document that belongs to a candidate
    $doc_id = DB::table('documents')
        ->select('id')
        ->where('candidate_id', $id)
        ->first()
        ->id;
    $data =  document::find($doc_id);

    return view('candidate.document', compact('data'));
}

candidate/document.blade.php:

<iframe src="/storage/uploaded/{{ $data->doc }}"></iframe>

提前谢谢你。

【问题讨论】:

  • 存储在根目录吗?您可能想为当前目录尝试./storage
  • 向我们展示您的文件/文件夹树。文件路径错误。
  • @infinitezero 不是它不在根目录中
  • / 指向根目录 ./ 指向当前目录,../ 反向指向父目录。

标签: php laravel file laravel-8


【解决方案1】:

运行php artisan storage:link后,检查&lt;project&gt;/public文件夹,应该有对应的软链接文件夹链接到你的存储应用文件夹。

例子

ls -la <project>/public
...
lrwxrwxrwx  1 admin   61 Aug  6 12:10 storage -> /home/xxx/storage/app/public/

【讨论】:

    【解决方案2】:

    保存文件:

    $file = $request->file('doc');
    $filename = time().'.'.$file->getClientOriginalExtension() ;
    $document->doc = $filename;
    Storage::disk('local')->put($file, $filename);
    

    访问文件

    {{asset('storage/'.$data->doc)}}
    

    【讨论】:

    • 我收到此错误“无法创建根目录”
    猜你喜欢
    • 2021-10-14
    • 1970-01-01
    • 2020-05-07
    • 2018-11-13
    • 2022-11-29
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多