Laravel 5.8破碎的图像上传

时间:2019-04-22 17:51:28

标签: php laravel eloquent

我在其中一个食谱应用程序中上传了一张图片,到目前为止,该图片一直运行良好。它突然坏了,如果不上传图像,我只能将其发布回默认的“ noimage.jpg”。有人可以看看我的ogic,看看我出了什么问题吗?我不明白它在哪里破裂。谢谢。

控制器存储功能

// Handle File Upload 
        if($request->hasfile('recipeImage')){
            // Get filename with extension
            $fileameWithExt = $request->file('recipeImage')->getClientOriginalName();
            // Get just filename
            $filename = pathinfo($fileameWithExt, PATHINFO_FILENAME);
            // Get just extension
            $extension = $request->file('recipeImage')->getClientOriginalExtension();
            // Filename to store
            $fileNameToStore = $filename . '_' . time() . '.' . $extension;
            // Upload Image
            $path = $request->file('recipeImage')->storeAs('public/recipe_images', $fileNameToStore);
        } else {
            $fileNameToStore = 'noimage.jpg';
        }


        $recipe = new Recipe;
            $recipe->author = auth()->user()->username;
            $recipe->title = $request->input('title');
            $recipe->description = $request->input('description');
            $recipe->ingredients = $request->input('ingredients');
            $recipe->directions = $request->input('directions');
            $recipe->recipeImage = $fileNameToStore;
            $recipe->prepTime = $request->input('prepTime');
            $recipe->cookTime = $request->input('cookTime');
            $recipe->servings = $request->input('servings');
            $recipe->calories = $request->input('calories');
            $recipe->user_id = auth()->user()->id;
            $recipe->save();

带有表单的刀片视图

<form method="POST" action="{{ action('RecipesController@store') }}" id="submitRecipeForm">
    <div class="photoUploadInput">
                    <label for="imageUpload">
                        <input type="file" id="imageUpload" name="recipeImage">
                    </label>
                    <span>Add a photo of your recipe!</span>
                </div>

如果您需要查看其他代码,请告诉我。谢谢

2 个答案:

答案 0 :(得分:2)

尝试将表单的enctype属性设置为multipart/form-data

<form method="POST" 
    action="{{ action('RecipesController@store') }}" 
    id="submitRecipeForm" 
    enctype="multipart/form-data">
...

答案 1 :(得分:1)

试一下:

php artisan cache:clear