Error mimetype application/octet-stream in jpeg image with Laravel

Asked

Viewed 354 times

-1

When trying to upload an image with Laravel I notice a different code behavior depending on the size of the image preventing the upload of the file as demonstrated in the image below that I get with the function dd() in Laravel;

inserir a descrição da imagem aqui

Code of my function to update the record with the image name to make the link in html;

public function update(Request $request, User $user, $id)
{
    try {
        DB::beginTransaction();
        $data = $request->all();
        $resource = $user->findOrFail((int)$id);
        $img = $request->file('image');

        if ($img) {        
            if ($img->isValid()) {
                $nameFile = hash('md5', now()->timestamp) . '.' . $img->extension();
                $upload = $request->file('image')->storeAs(
                    'marca', "$nameFile"
                );
                if (!$upload) {
                    throw new Exception('Falha ao enviar imagem!', '500');
                }
                $data['image'] = $nameFile;
            } else {
                dd(
                    $img,
                    $img->isValid(),
                    $img->getClientMimeType()
                );
            }
        }
        $resource->update($data);
        DB::commit();
    } catch (Exception $exception) {
        DB::rollBack();
        return redirect()->back()->withErrors($exception->getMessage());
    }

    return redirect("/user/$resource->id")->with('saved', "success");
}

But if I open the same image in Paint and reduce the size in percentage of the image to 25% I can upload the file normally.

Could someone tell why the code behaves differently in relation to the image size?

Print out the image details I can’t upload; inserir a descrição da imagem aqui

Image Properties edited in Paint that I CAN upload; inserir a descrição da imagem aqui

  • This is a server problem, not the Laravel problem, the Laravel site has an api for you to manage the strings by the get method.

2 answers

0

see: https://king.host/wiki/artigo/mimetypes/

Configure mimetypes on Linux server If your hosting environment is Linux Apache, please use the . htaccess file to add file extensions to your website. Follow an example below (mp4 extension):

Extension of mp4 video

AddType video/mp4 .mp4

change video/mp4 to what you are wanting to allow, more take care, see it if you do not mess up and opens a security breach of the server.

Lista com os mime-types mais comuns
Extensão    Mimetype    Tipo de arquivo
.7z application/7z  7-Zip
.aac    audio/x-aac Áudio
.apk    application/vnd.android.package-archive Aplicativo Android
.avi    video/x-msvideo Vídeo
.bin    application/octet-stream    Binário
.css    text/css    Folhas de estilos
.doc    application/msword  Documento de texto
.exe    application/octet-stream    Pacotes executáveis
.html   text/html   Hipertexto
.js application/x-javascript    Javascript
.mp4    video/mp4   Vídeo
.pdf    application/pdf PDF
.swf    application/x-shockwave Adobe Shockwave
.zip    application/zip Compactação

-2

Opa,

I had this same problem, and as a last attempt I updated Laravel via Composer (update in the project folder) and to my surprise there was a mime update, guess what? Everything is back to normal and it is life that follows! Thank you!

Att

Browser other questions tagged

You are not signed in. Login or sign up in order to post.