jeudi 23 mai 2019

Function for securely saving photos on the server in Laravel 5.8 - error FileException

I am very beginner in Laravel. I have code for securely save photos on the server in my Controller:

public function storeDJS(Request $request)
    {
        if ($request->hasFile('myFile')) {
            $allowedfileExtension = ['jpg', 'jpeg'];
            $files = $request->file('myFile');
            foreach ($files as $file) {
                $filename = strtolower($file->getClientOriginalName());
                $extension = strtolower($file->getClientOriginalExtension());
                $check = in_array($extension, $allowedfileExtension);
                if ($check) {
                    foreach ($request->myFile as $file) {
                        $uniqueName = $file->move(public_path('upload/images'), md5($file . time()) . '.' . $extension);
                        echo "XXXX 5";
                        // save to DB
                    }
                }
            }
        }
    }

The above code works when it uploads 1 file. When I upload a pair of files I get the error: FileException The file "1558605021395IMG_3445.JPG" was not uploaded due to an unknown error.

How can I add to my function validation:

$this->validate($request, [
      'image' => 'required|image|mimes:jpeg,jpg|max:8192',
    ]);

??



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire