laravel

时间:2019-06-02 19:25:52

标签: php laravel

文件成功上传后,我的show.blade似乎找不到要显示的图像。

我已经链接了目录,并且图像直接上传到所需的目录。

后控制器

public function store(Request $request)
    {
        /**
         * Validation Rules using Validator class
         */

        $rules = [
            /*'post_id'  =>   'required|numeric',*/
            'title'    =>   'required|min:3',
            'intro'    =>   'required|min:3',
            'content'  =>   'required|min:3',
            'image.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'    
        ];

        $validator = Validator::make($request->all(),$rules);

        if($validator->fails()) {
            // dd('validation fail');
            return Redirect::back()
            ->withInput()
            ->with(
                [
                'notification' =>'danger',
                'message' => 'Something went wrong'
                ]
            )
            ->withErrors($validator);

        }        
        $post = new Post();
        $post->title = $request->input('title');
        $post->intro = $request->input('intro');
        $post->content = $request->input('content');
        $success = $post->save();


        /**
         * Handle image upload
         */

        if($request->hasfile('image') && $success){


            $directory = '/post-' . $post->id;

            foreach($request->file('image') as $image) {

                $name = $image->getClientOriginalName();

                $extension = $image ->getClientOriginalExtension();

                $fileName = pathinfo($name,PATHINFO_FILENAME) . '-' . time() . '.' . $extension;

                $image->storeAs($directory,$fileName,'public');

                $image = new Image();
                $image->post_id = $post->id;
                $image->filename = $fileName;
                $image->filepath = $directory;
                $image->save();
            }

                return back()->with([
                    'notification' => 'succes',
                    'message' => 'You have created a new post'
                ]);

            }

        }

显示刀片

@extends('layout')

@section('content')

<div>
<h1 class="title">{{$post->title}}</h1>

<p> {{$post->content}} </p>
<div class="row">
@foreach($post->images as $image)
<div class="col-sm">
<img class="img-fluid" src="{{  asset($image->filepath . '/' . $image->filename) }}" alt="{{ $post->title }}">
</div>

@endforeach
</div>
</div>

<p><a href="/posts/{{ $post->id }}/edit">Edit</a></p>
@endsection

当我检查元素时,会得到图像的alt,即$ post->标题,但是由于某种原因,路径似乎不正确。

2 个答案:

答案 0 :(得分:0)

尝试一下。

<img class="img-fluid" src="src="{{ asset('path/to/image/'. $image->filename) }}"" alt="{{ $post->title }}">

我宁愿这样批准

获取最后插入的帖子ID

$post_id = post->id;
$image = $request->file('image');

 if ($request->hasFile('image'))
  {
    foreach ($request->image as $img) {

            $name = $image->getClientOriginalName();

            $extension = $image->getClientOriginalExtension();

            $fileName = $name . '-' . time() . '.' . $extension;

            if (!file_exists('POST/IMAGE')) //Let check if the path doesn't exist
            {
                mkdir('POST/IMAGE', 0777 , true);// then create one with read/write permission
            }
            $image->move('POST/IMAGE',$imagename); // Save the file in this folder
        }else {
            $imagename = 'dafault.png'; // Optional: if none file selected, the use default file.
        }

            $image = new Image();
            $image->post_id = $post_idid;
            $image->filename = $fileName; 
            //$image->filepath = $directory;// You don't really need the save the file name in your DB
            $image->save();
        }

            return back()->with([
                'notification' => 'succes',
                'message' => 'You have created a new post'
            ]);

        }

答案 1 :(得分:0)

sums = [] for key in est_dict.keys(): model, alpha = est_dict[key] fX = model.predict(X) y_fx = np.where(fX==0, -1, fX) sums.append(y_fx * alpha) 函数返回相对于存储器的已存储文件的路径。您不需要自己连接它。路径中的正斜线也可能是一个问题。

storeAs

仅在该路径上使用// Store result path to the variable $path = $image->storeAs($directory,$fileName,'public'); $image = new Image(); $image->post_id = $post->id; $image->filename = $fileName; $image->filepath = $path; // Save path returned by the storeAs $image->save();

asset

还要检查您的file system configuration<img class="img-fluid" src="{{ asset($image->filepath) }}" alt="{{ $post->title }}"> 应该是指向public/storage的符号链接,否则将无法从网上访问存储的文件。