【问题标题】:Image upload on laravel doesn't work onlinelaravel上的图片上传无法在线使用
【发布时间】:2020-04-02 08:34:28
【问题描述】:

我完成了我的项目,当我将它上传到我的在线服务器时,他们不允许你更改根地图,所以我使用 .htaccess 文件直接访问公共地图,但是当我提交表单时,我不能上传后打开图片,在目录中找不到,这是我的控制器代码

if ($request->hasFile('image')) {
            $filenameWithExt = $request->file('image')->getClientOriginalExtension();
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
            $extension = $request->file('image')->getClientOriginalExtension();
            $fileNameToStore = $filename . '_' . time() . '.' . $extension;
            $request->file('image')->storeAs('public/images', $fileNameToStore);
        } else {
            $fileNameToStore = 'noimage.jpg';
        }

这是上传的 Html

<div class="form-group">
    <label>Upload foto's</label>
       <div class="custom-file">
           <input type="file" name="image" class="custom-file-input" id="inputGroupFile01" aria-describedby="inputGroupFileAddon01" multiple>
           <label class="custom-file-label" for="inputGroupFile01">Selecteer bestanden</label>
     </div>
</div>

这是我展示或下载的 HTML

<tr>
   <th>Foto's</th>
        @if( $retour->images === 'noimage.jpg')
           <td>Geen foto beschikbaar</td>
        @else
           <td><a download="retourmelding_{{$retour->firmaname}}"
               href="/storage/images/{{$retour->images}}" title="Foto">
               <img alt="Foto" src="/storage/images/{{$retour->images}}">
               </a>
           </td>
        @endif
</tr>

这在本地完美,但在网上不行,请帮帮我

【问题讨论】:

    标签: laravel image upload public


    【解决方案1】:

    当您获取上面在 href 中指定的文件时。在本地它可以工作,因为您的本地计算机可以在 href 中呈现 /storage 文件夹路径,但在实时服务器中,您需要像这样指定:

    {{ URL::to('/') }}/images/stackoverflow.png
    

    现在看看这个解决方案:

    通过以下命令链接您的存储文件夹:

    php artisan storage:link 
    
    

    像这样从刀片文件中访问文件:

    
    <td>
    <a download="retourmelding_{{$retour->firmaname}}" href="{{ URL::to('/') }}/public/storage/images/{{$retour->images}}" title="Foto">
    <img alt="Foto" src="{{ URL::to('/') }}/public/storage/images/{{$retour->images}}"></a>
    </td>
    
    
    
    
    

    【讨论】:

    • 它不起作用,当我点击显示它时,我的浏览器尝试访问mywebsitepublic/storage/images/noimage.jpg
    • 在 public 之前放一个 /。我已经编辑了答案检查
    • 谢谢,现在当我点击它访问正确的链接,但它找不到它,它说Firefox can’t find the file at http://mywebsite/public/storage/images/jpg_1575886458.jpg.
    • 你链接你的存储文件夹了吗?通过使用命令 php artisan storage:link
    • 是的,[37;41m“public/storage”目录已经存在。?[39;49m
    猜你喜欢
    • 1970-01-01
    • 2020-03-21
    • 2019-11-13
    • 2021-07-01
    • 2014-12-22
    • 2020-10-03
    • 2018-04-17
    • 2017-08-01
    • 1970-01-01
    相关资源
    最近更新 更多