简体   繁体   English

如何在实时服务器上显示 Laravel 图像?

[英]How can I display Laravel images on a live server?

I developed a laravel app which is working perfectly on my local machine together with the images.我开发了一个 Laravel 应用程序,它与图像一起在我的本地机器上完美运行。 On deployment to live server, the images are no longer visible.在部署到实时服务器时,图像不再可见。 I am using shared hosting and my folder structure is like this: public_html containing the public files like upload and main containing the remaining files which should not be visible to the user.我正在使用共享主机,我的文件夹结构是这样的: public_html包含公共文件,如上传和main包含用户不应该看到的剩余文件。 Here is my controller `public function这是我的控制器`公共功能

PortfolioAdd(){
        return view('frontendbackend.portfoliosection.addportfolio');
    }

    public function PortfolioStore(Request $request){

            $validatedData = $request->validate([
                'title' => 'required:portfolio_sections,title',
                'description' => 'required:portfolio_sections,description',
            
            
        ]);

        $data = new PorfolioSection();
        $data->title = $request->title;
        $data->description = $request->description;

        if ($request->file('image')) {
            $file = $request->file('image');
            $filename = date('YmdHi').$file->getClientOriginalName();
            $file->move(public_path('upload/portfolio_images'),$filename);
            $data['image'] = $filename;
        }
     
        $data->save();

`and here is my view `这是我的观点

                  <table id="example1" class="table table-bordered table-striped" style = "color:white">
                    <thead>
                    <tr>
            <th width="5%" style = "color:white">SL</th>
            <th style = "color:white">Title</th>
            <th style = "color:white">Description</th>
            <th style = "color:white">Image</th>
            <th style = "color:white">Action</th>    
        </tr>
    </thead>
    <tbody> 
    @foreach($allData as $key => $portfolio )
        <tr>
            <td style = "color:white"> {{ $key+1 }} </td>
            <td> {{ $portfolio->title }} </td>
            <td> {{ $portfolio->description }} </td>
            <td> 
                
                <img src="{{ (!empty($portfolio->image))? url('upload/portfolio_images/'.$portfolio->image):url('upload/no_image.jpg') }}" style="width: 60px; width: 60px;"> 
                           </td>    

            
            <td>
            <a href="{{route('view.portfolio.edit', $portfolio->id)}}" class="btn btn-info">Edit</a>
            <a href="{{route('view.portfolio.delete', $portfolio->id)}}" class="btn btn-danger" id="delete">Delete</a>

            </td>
        </tr>
        @endforeach
                    </tbody>
                    <tfoot>
                         
                    </tfoot>
                  </table>

First check whether ($portfolio->image) is showing any value by passing it directly in the tag ie首先检查 ($portfolio->image) 是否通过直接在标签中传递它来显示任何值,即

{{ $portfolio->image}} {{ $portfolio->image}}

if it returns values, check whether the value exists in your folder.如果它返回值,请检查该值是否存在于您的文件夹中。 If it exists ch then check your image src.如果它存在 ch 然后检查你的图像 src。

I would try the following(have not tested)我会尝试以下(尚未测试)

change this: $file->move(public_path('upload/portfolio_images'),$filename);改变这个: $file->move(public_path('upload/portfolio_images'),$filename);

to: $file->move(public_path() . 'upload/portfolio_images',$filename);到: $file->move(public_path() . 'upload/portfolio_images',$filename);

You must create a symbolink link for that.您必须为此创建一个符号链接。 The default folder for uploading files is the storage folder.上传文件的默认文件夹是存储文件夹。 By running php artisan storage:link, the application will generate a symbolic link that files will be accebile from the public area.通过运行 php artisan storage:link,应用程序将生成一个符号链接,文件将从公共区域访问。 Google symbolic link.谷歌符号链接。 You will find the answer.你会找到答案。

the easiest way to do it is to create a folder inside your public folder eg portfolio_images.最简单的方法是在公共文件夹中创建一个文件夹,例如portfolio_images。 then然后

        if ($request->has('photo')) {
            $photoName= time() . 'photo' . $request->receipt->extension();
            $request->photo->move(public_path('portfolio_images'), $photoName);
        }

This will save it in public>portfolio_images.这会将其保存在 public>portfolio_images 中。 Then in your view write <img id="previewImg" src="{{ asset("portfolio_images/$portfolio->image") }}" alt="Preview your image" height="150"> This should solve it!然后在您的视图中写入 <img id="previewImg" src="{{ asset("portfolio_images/$portfolio->image") }}" alt="Preview your image" height="150"> 这应该可以解决!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM