MattB's avatar
Level 2

Odd image upload issue

I have an odd issue with displaying the result of a file upload form. Below is the code for the upload:

public function store(Request $request){
        //
        if($file = $request->file('image')){
          $name = $file->getClientOriginalName();
          $path = 'images';
          if($file->move($path, $name)){
            $post = new Gallery();
            $path = '../public/images';
            $post->image = $path . '/' . $name;
            $post->name = $request->name;
            $post->species_id = $request->species_id;
            $post->tag = $request->tag;
            $post->patreon = $request->patreon;
            $post->save();
            return redirect()->route('admin.gallery.index');
          };
        };
    }

It uploads all the images ok, but when I display them with the following code, some will show on the page as expected, but some show a 404 error. They are showing in the folder specified ok and in the db, it's showing the correct file path (correct path in the Chrome dev console too). I have no idea what's going on here.

@extends('layouts.app')

@section('content')
  <div class="container galleryContainer">
    <br>
    <div class="display-4">Gallery</div><br>
    <ul class="galleryList">
      @foreach($mainGallery as $gallery)
      <li><p>{{$gallery->name}}</p><a href="{{$gallery->image}}" target="_blank"><img src="{{$gallery->image}}" alt="" class="img-thumbnail" width="200" height="400"></a></li>
      @endforeach
    </ul>
  </div>
  @endsection
0 likes
11 replies
MattB's avatar
Level 2

@MICHALURVA - I tried it as

<li><p>{{$gallery->name}}</p><a href="asset('images/'.$gallery.name);" target="_blank"><img src="asset('images/'.$gallery.name);" alt="" class="img-thumbnail" width="200" height="400"></a></li>

but now none of the pictures show. Have I misunderstood massively?

michalurva's avatar

I am sorry, my bad should be {{ asset('images/' . $gallery->name) }}

MattB's avatar
Level 2

Ah that works to bring the images back but still not all. Some still show as 404

michalurva's avatar

To store file try $file->move( public_path() . '/images/', $name);

MattB's avatar
Level 2

That didn't work I'm afraid. What's odd is that the same images every time fails to load. When it does work, it's always the same ones that do work. I bet it's some issue with the image but I fail to see what

MaverickChan's avatar

@MATTB - just add a / before image

$path = '/images';

delete the line inside

$path = '../public/images'

you don't need it

MattB's avatar
Level 2

@MAVERICKCHAN - I'm sorry that still didn't work. I need that line due to the image being shown on the admin page as per:

@foreach($gallery as $images)
    <tr>
      <td width="20%"><img height="50" src="{{$images->image}}"></td>
      <td>{{$images->name}}</td>
      <td>{{$images->species['name']}}</td>
      <td>{{$images->tag == 1 ? 'Yes' : 'No'}}</td>
      <td>{{$images->patreon == 1 ? 'Yes' : 'No'}}</td>
      <td width="10%">
        {!! Form::open(['method'=>'DELETE', 'action'=> ['GalleryController@destroy', $images->id]]) !!}
        <div class="form-group">
          {!! Form::submit('Delete', ['class'=>'btn btn-danger']) !!}
        </div>
        {!! form::close() !!}
      </td>
    </tr>
    @endforeach

If I use {{ asset('images/' . $gallery->image) }} in here, I get error "Property [image] does not exist on this collection instance."

mvd's avatar

@mattb can you check de image filenames? Because some images does work well so i think the problem is not in de code.

MaverickChan's avatar

@MATTB - the $path='../public/images' is really not right

change it to '/images',

because where you upload to , is public. When you use '../public' it turns to 'public/public', so you will never find your image.

Dalma's avatar

If you have not already done so, check to see the URL being presented for images that work and those that don't work and compare paths and domain names. Hopefully something should be apparent and from that we can work to better assist you.

Please or to participate in this conversation.