1

I have successfully uploaded this formats (jpeg, png, gif) buh not jpg format is not being upload.

public function store(Request $request)
{
  $images = array();
  if($request->hasFile('images')){
    foreach ($request->file('images') as $image) {
        $path = $image->store('images');
        $images[] = $path;
    }
  }
  dd($request->file('images'));
}

My problem is just that! why is JPG format unable to upload.

edit dd out the following:

 array:1 [▼
    0 => UploadedFile {#264 ▼
    -test: false
    -originalName: "home.jpg"
    -mimeType: "application/octet-stream"
    -error: 1
    #hashName: null
    path: ""
    filename: ""
    basename: ""
    pathname: ""
    extension: ""
    realPath: "/home/vic/dev/project/client/ccity/public"
    aTime: 1970-01-01 00:00:00
    mTime: 1970-01-01 00:00:00
    cTime: 1970-01-01 00:00:00
    inode: false
    size: false
    perms: 00
    owner: false
    group: false
    type: false
    writable: false
    readable: false
    executable: false
    file: false
    dir: false
    link: false
  }
]
Assad Ullah Ch
  • 706
  • 7
  • 12
  • there is nothing in this code that would exclude any file types – lagbox Nov 20 '19 at 08:03
  • 3
    Had a same problem few days ago. After the full day of debugging, I realized that the `max_upload_size` in `php.ini` configuration wasn't enough for the images I was uploading. Default is `2MB` I think, so that could be your issue. Make it bigger and try again. – zlatan Nov 20 '19 at 08:09
  • before what @zlatan suggested, try uploading a small `jpg` file to confirm that it's a file size issue – N69S Nov 20 '19 at 08:27
  • does this answer your question? https://stackoverflow.com/questions/45775532/image-upload-not-work-laravel-5-4-doesnt-get-any-error – Oluwatobi Samuel Omisakin Nov 20 '19 at 08:35
  • This work like magic but it is upload_max_filesize in php 7. Thanks @zlatan – Akan Udosen Nov 20 '19 at 09:19
  • No problem, glad I could help. – zlatan Nov 20 '19 at 09:22
  • Does this happen because the file is send binary via `application/octet-stream`? Keep in mind that the file extension isn't necessarily the MIME type – shaedrich May 26 '21 at 15:11

4 Answers4

2

You should add

$this->validate($request, [
    'images'             => 'required|image|mimes:jpeg,png,jpg,gif'
 ])
Hamza
  • 300
  • 1
  • 9
0

Check your blade file by added this:

<input type="file" accept="image/*" /> <!-- all image types --> 
Sok Chanty
  • 1,678
  • 2
  • 12
  • 22
  • It's not a good solution as the DOM can easily be modified using browser dev tools. The server side is the real deal. The better solution is to combine both client side and server side validation. – Assad Ullah Ch Nov 20 '19 at 10:29
0

Let me walk you through every step so you don't miss out anything.

Step 1: Create a controller to show the form and handle file uploading

class FileUploadController extends Controller
{
    public function showUploadForm(){
        return view('form');
    }

    public function upload( Request $request ){

        $this->validate( $request, [
            'images' => 'required',
            'images.*' => 'image|mimes:jpeg,png,jpg,gif'
        ]);

        $images = [];

        foreach($request->file('images') as $image){
            $images[] = $image->store('image');
        }

    }
}

Step 2: Define routes on web.php file:

Route::get('/upload', 'FileUploadController@showUploadForm');
Route::post('/upload', 'FileUploadController@upload');

Step 3: File uploading form:

<form action="/upload" enctype="multipart/form-data"  method="post">
    @csrf
    <input type="file" name="images[]" multiple>
    <button>Upload</button>
</form>

Now if you upload one or more files, they all should be uploaded in storage/app/images/ directory.

Note: Don't forget to make sure the value of upload_max_filesize on php.ini file. You can specify some bigger value so the large file sizes can be uploaded easily.

Let me know if you still need help with this.

Assad Ullah Ch
  • 706
  • 7
  • 12
0

If you are validating the data make sure that you replaced the |image| with |mimes:..|. As following

$this->validate($request, [
    'images'             => 'required|mimes:jpeg,png,jpg,gif'
 ])

Notice that I'm not using the |image| in the validation of the image instead I used mimes:jpeg,png,jpg,gif because for some reason |Image| accepts all the image formats except jpg/jpeg. Happy coding!