Reputation: 75
I'm developing a simple web app using Laravel.
and I want to add an image upload and stirage function.
I want to save the binary data of the image to the DataBase.
But there is an error message.
file_get_contents(): Filename cannot be empty
How can I solve this issue?
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateAttachmentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('attachments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('path');
$table->text('image');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('attachments');
}
}
@extends('layouts.front2')
@section('title','mainpage')
@section('content')
<link rel="stylesheet" href="{{ asset('css/main2.css') }}">
<div class="profile">
<div class="name">
@guest
<a class="nav-link2" href="{{ route('register')}}">{{ __('Create Accout!')}}</a>
@else
<a id="navbarDropdown" class="nav-link2" href="#" role="button">
{{ Auth::user()->name }}<span class="caret"></span></a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
@csrf
</form>
@endguest
</div>
<div class="aboutme">
<tbody>
@foreach($posts as $profile)
<tr>
<td>{{ \Str::limit($profile->title, 100) }}</td>
<td>{{ \Str::limit($profile->body, 250) }}</td>
</tr>
<a href="{{ action('ProfileController@delete', ['id' => $profile->id]) }}">delete</a>
<a href="{{ action('ProfileController@update', ['id' => $profile->id]) }}" class="update">update</a>
@endforeach
</tbody>
<br>
</div>
</div>
<div class="new">
<div class="newtitle">
<h1>New</h1>
</div>
<div class="container1">
@foreach ($images as $image)
<img src="data:image/png;base64,{{ $image->image }}" class="images" style="height: 250px; width: 250px; border-radius: 50%;">
<a href="{{ action('StoriesController@delete', ['id' => $image->id]) }}">delete</a>
@endforeach
<div class="more">
more...
</div>
</div>
</div>
{{ csrf_field() }}
@endsection
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Story;
use App\Profile;
use Auth;
use App\Posts;
use App\History;
use App\Attachment;
use Carbon\Carbon;
use Storage;
class StoriesController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index(Request $request)
{
$images = Attachment::all();
$cond_title = $request->cond_title;
if ($cond_title != '') {
$posts = Profile::where('title', $cond_title)->get();
} else {
$posts = Profile::all();
}
return view('stories.index2', compact('images','posts','cond_title'));
}
public function add()
{
return view('stories.create2');
}
public function store(Request $request)
{
$image = new Attachment();
$image->image = base64_encode(file_get_contents($request->image));
$image->save();
}
Upvotes: 1
Views: 50
Reputation: 594
You need to get the path where the temporary image is stored. To do that do
$request->file('image')
so your code becomes
public function store(Request $request)
{
$image = new Attachment();
$image->image = base64_encode(file_get_contents($request->file('image')));
$image->save();
}
Upvotes: 1