简体   繁体   中英

Upload image on laravel 5.3 but got null

I am making a feature on website that can let user upload image and image will store in project's directory. Image path also created and stored in database so that can use the path to get image.

This site uses Laravel 5.3 with PHP 7 and MariaDB 10.1.6 on Windows Server 2008 R2 now. When I trying to upload, I found that there's no any images and path stored in directory and database, database's image path ( which I named as imgsrc ) column only stored NULL even I choose the image on .

Here's my view part:

<form action="/task" method="POST" class="form-horizontal" enctype="multipart/form-data">
<!-- Bypass other input part -->
    <div class="row">
        <label for="task-image" class="col-sm-3 control-label">Image</label>
        <div class="col-sm-6">
            <input type="file" name="imgsrc" id="task-image" class="form-control"  accept="image/*">
        </div>
    </div>
<!-- Bypass other input part -->
</form>

And here's my controller part:

<?php
namespace App\Http\Controllers;
use Image;    // Intervention\Image
use App\Task;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class crud_controller extends Controller
{
    public function create_crud (Request $request)
    {
        /* Bypass other part */
        if ( $request->hasFile('image') )
        {
            if ( $request->hasFile('photo') )
            {
                $filename = rand(10,100).$request->file('photo')->getClientOriginalName();
                $formal_file = 'storage/images/'.$filename;
                $img_file = Image::make( Input::file('photo') )
                            ->resize('400',null)->save($formal_file);
                // Fllowing part, none of all will fix problem.
                // $image->save(storage_path('app/blogpost/' . $postId . '/' . $imageName));
                // storage_path('storage/images'.);
                // $img_file = Image::make( Input::file('photo') ->resize('400',null)->move(public_path().'/images/vijesti', $filename);
                $task->imgsrc = $request->$formal_file;
            }
        }
        $task->save();
        return redirect('/');
    }
    /* Bypass other part */
}

I read Laravel's documentation and some of question on Stack Overflow, tried to use them. However, database's imgsrc column still stores NULL and no directory stored images.

What's wrong with my code?

Your if statement will always fails because your image name is imgsrc but you are checking for image/photo so you can change your code to following:

if ( $request->hasFile('imgsrc') )
{
    $filename = rand(10,100).$request->file('photo')->getClientOriginalName();
    $formal_file = 'storage/images/'.$filename;
    $img_file = Image::make( Input::file('photo') )
                ->resize('400',null)->save($formal_file);
    // Fllowing part, none of all will fix problem.
    // $image->save(storage_path('app/blogpost/' . $postId . '/' . $imageName));
    // storage_path('storage/images'.);
    // $img_file = Image::make( Input::file('photo') ->resize('400',null)->move(public_path().'/images/vijesti', $filename);
    $task->imgsrc = $request->$formal_file;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM