I have successfully uploaded the image files to the server using Laravel 5.0. Here is my form:
<html>
<head>
<title>Uploads</title>
</head>
<body>
{!! Form::open(array('url' => 'uploadProcessing' , 'enctype' => 'multipart/form-data')) !!}
{!! Form::label('image','Upload Image') !!}
{!! Form::file('image') !!}
{!! Form::submit('Submit') !!}
{!! Form::close() !!}
</body>
</html>
Here is my Controller:
public function uploadProcessing() {
$image = Input::file('image');
$imageName = rand(1111, 9999) . '.' . Input::file('image')->getClientOriginalExtension();
Input::file('image')->move(base_path() . '/public/uploads/', $imageName);
}
Along with that, I am saving the $imageName as a refrence in database table.
Now I have to display that image in a view with that reference. I am trying to access it in this way:
#foreach($result as $r)
{!! HTML::image('uploads/$r->reference') !!}
#endforeach
But it is not working, any help?
I got it, It was simple, I tried this:
<img src="..\uploads\{!! $r->reference !!}" />
And it worked
Related
This is my form
#extends('layout.template')
#section('content')
<h1>Add Student</h1>
{!! Form::open(array('action' => 'studentController#save', 'files'=>true)) !!}
<div class="form-group">
{!! Form::label('Profile-Picture', 'Profile Picture:') !!}
{!! Form::file('image',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Save', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
#stop
This is my controller method
public function save()
{
$students=Request::all();
students::create($students);
Session::flash('flash_message', 'Record successfully added!');
return redirect('students');
}
when i upload image and submit image than in the database column field save this image address "/tmp/phpFFuMwC";
That' s because you are saving the temporarily generated file url.
For files you need to manually save it to the desired location
( make sure it passes validations ):
$request->file('photo')->move($destinationPath);
// With a custom filename
$request->file('photo')->move($destinationPath, $fileName);
and then store the new filename ( with or without the path ) in the database something like this:
$students = new Students;
$students->image = $fileName;
...
$students->save();
Docs: https://laravel.com/docs/5.2/requests#files
On your controlller make these changes
public function save(Request $request)
{
$destination = 'uploads/photos/'; // your upload folder
$image = $request->file('image');
$filename = $image->getClientOriginalName(); // get the filename
$image->move($destination, $filename); // move file to destination
// create a record
Student::create([
'image' => $destination . $filename
]);
return back()->withSuccess('Success.');
}
Don't forget to use
use Illuminate\Http\Request;
I am new to laravel.I am trying to create a basic form in my form.blade.php page.But when i try to navigate to my form.blade.php page i am getting the following error:
FatalErrorException in 5fca28a34b9eeee32d99bfd5ad77d6a463cb98c9.php
line 23: syntax error, unexpected 'put' (T_STRING), expecting ')'
in my route.php i am calling the View::make() method to get to the form.blade.php page
Route::get('/',function(){
return View::make('form');
});
form.blade.php page::
<!DOCTYPE html>
<html>
<body>
{{Form::open(array('url'=>'thanks'))}}
{{Form::label('email','Email Address')}}
{{Form::text('email')}}
{{Form::label('os','operating system')}}
{{Form::select('os',array(
'linux'=>'Linux',
'mac'=>'Mac',
'windows'=>'windows'
))}}
{{Form::label('comment','Comment')}}
{{From::textarea('comment','',array('placeholder=>'put your comment here'))}}
{{Form::checkbox('agree','yes',false)}}
{{Form::label('agree','i agree with your terms and condition')}}
{{Form::submit('submit')}}
{{Form::close()}}
</body>
</html>
{!! Form::open(array('url'=>'thanks')) !!}
{!! Form::label('email','Email Address') !!}
{!! Form::text('email') !!}
{!! Form::label('os','operating system') !!}
{!! Form::select('os',array(
'linux'=>'Linux',
'mac'=>'Mac',
'windows'=>'windows'
)) !!}
{!! Form::label('comment','Comment') !!}
{!! Form::textarea('comment','',array('placeholder'=>'put your comment here')) !!}
{!! Form::checkbox('agree','yes',false) !!}
{!! Form::label('agree','i agree with your terms and condition') !!}
{!! Form::submit('submit') !!}
{!! Form::close() !!}
Above is the correct way to use form/html, here is the documentation laravelcollective/html
{{From::textarea('comment','',array('placeholder=>'put your comment here'))}}
should be
{{From::textarea('comment','',array('placeholder'=>'put your comment here'))}}
Seems like you are missing a single quote.
I need help in saving uploaded file name in database table using laravel 5.1.
My Controller code for saving Image details
public function store(Request $request)
{
if($request->hasFile('img_filename'))
{
$destinationPath="offerimages";
$file = $request->file('img_filename');
$filename=$file->getClientOriginalName();
$request->file('img_filename')->move($destinationPath,$filename);
}
$input=$request->all();
Offer_image::create($input);
return redirect('offerimage');
}
My view code for accepting image
{!! Form::open(array('route'=>'offerimage.store','role'=>'form','files'=>true)) !!}
<div class="box-body">
<div class="form-group">
{!! Form::label('img_name','Name') !!}
{!! Form::text('img_name', $value = null, $attributes = array('class'=>'form-control','id'=>'img_name','required')) !!}
</div>
<div class="form-group">
{!! Form::label('img_description','Description') !!}
{!! Form::textarea('img_description', $value = null, $attributes = array('class'=>'form-control','id'=>'img_description','required')) !!}
</div>
<div class="form-group">
{!! Form::label('img_filename','Upload Image') !!}
{!! Form::file('img_filename') !!}
</div>
{!! Form::hidden('status',$value='active') !!}
</div><!-- /.box-body -->
<div class="box-footer">
{!! Form::submit('Submit',$attributes=array('class'=>'btn btn-primary')) !!}
</div>
{!! Form::close() !!}
This controller code to store image working properly, but where i am trying to save image file name to table , this code is storing filepath to database table.
As I am using direct create() method to store the request object in table, I don't know how do I store file name instead of path.
Check this Image for table data
The Problem is that your Request Data hasn't changed while you uploaded the picture. So img_filename still contains tmpdata.
You can try this:
$input = $request->all();
$input['img_filename'] = $filename;
Code that works for me :
$updir = 'images/';
$img_name = 'image.jpeg';
Request::file('img_filename')->move($updir, $img_name);
$file = $request->file('img_filename');
$filename=$file->hashName();
Above is the hash name Laravel uses to save your files
I'll copy the relevant code and then i'll explain the problem.
routes.php
get('/movies/create', 'MovieController#create');
Moviecontroller.php
public function create()
{
return view('movies.create');
}
master.blade.php
<html>
<head>
<title>Laravel</title>
</head>
<body>
#yield('content')
</body>
</html>
edit.blade.php
#extends('master')
#section('content')
<h1>Edit {{ $movie->name }}</h1>
{!! Form::model($movie, ['url' => '/movies/' . $movie->id, 'method' => 'PATCH']) !!}
{!! Form::text('name') !!}
{!! Form::text('director') !!}
{!! Form::textarea('description') !!}
{!! Form::text('rating') !!}
{!! Form::submit('Update movie') !!}
{!! Form::close() !!}
#stop
show.blade.php
#extends('master')
#section('content')
<h1>{{ $movie->name }}</h1>
<h4>Director: {{ $movie->director }}</h4>
<h4>Rating: {{ $movie->rating }}</h4>
<p>Description: {{ $movie->description }}</p>
{!! link_to('/movies/' . $movie->id . '/edit', 'Edit') !!}
#stop
so i have this code, but when i go to /movies/create in the browser, it's trying to open show.blade.html, which, of course, will throw an exception ($movie does not exist). Why does that happen?
You probably have a conflicting route above the one you showed, something like this:
get('/movies/{movie}', 'MovieController#show');
get('/movies/create', 'MovieController#create');
So when you go to yoursite.com/movies/create in your browser, the first route is triggered, and the controller opens show.blade.php - but there is no movie for it to show yet.
If you move them the other way around, the create method will work as intended, and you'll still be able to show existing movies:
get('/movies/create', 'MovieController#create');
get('/movies/{movie}', 'MovieController#show');
best way:
php artisan make:controller MovieController
and define route
Route::resource('movies', 'MovieController');
//available routes
Verb Path
GET /movies
GET /movies/create
POST /movies
GET /movies/{id}
GET /movies/{id}/edit
PUT/PATCH /movies/{id}
DELETE /movies/{id}
I'm stuck..
I want to add to give the possibity to add a new photo through a form.
I have the Photo resource properly set up.
The table photo with: id, title, caption, path:string.
This is the form that i made until now in the view photo.create:
{{ Form::open(array('route' => 'photos.store'),'image/save', array('files'=> true)) }}
{{ Form::label('title', 'Title: ') }}
{{ Form::text('title') }}
{{ Form::label('caption', 'Caption: ') }}
{{ Form::textarea('caption') }} <br>
{{ Form::submit('Add Photo', array('class' => 'btn btn-primary' )) }}
{{ Form::close() }}
How can i add a button thanks to which select a new file?
Thank you!!
EDIT:
This is the simple store method i made until now:
public function store()
{
$input = Input::all();
$rules = array('title' => 'required', 'path' => 'required');
$validation = Validator::make($input, $rules);
if ($validation->passes())
{
$photo = new Photo();
$photo->title = $input['title'];
$photo->caption = $input['caption'];
$photo->path = $input['path'];
$photo->save();
return Redirect::route('photos.index');
}
return Redirect::back()->withInput()->withErrors($validation)->with('message','There were validation message');
}
How can i put correctly there that implementation to retrieve the file and store in the folder located in public/img ?
And then how can i save that path and put into $photo->path ?
Thank you very much!!
Use Form::file('image')
Then, you can retrieve your uploaded file with Input::file('image') and move it to a destination with Input::file('file')->move(YOUR_DESTINATION_PATH);
References : http://laravel.com/docs/requests#files and http://laravel.com/docs/html#file-input
edit :
To store your uploaded file to public/img : Input::file('file')->move(base_path() . '/public/img');
Storing path in database :
$photo->path = base_path() . '/public/img' . Input::file('file')->getClientOriginalName(); // if you want your real path on harddrive
or
$photo->path = URL::to('img/' . Input::file('file')->getClientOriginalName()); // if you want an exploitable path for http render
second edit
See my correction on your form http://paste.laravel.com/KX9
#extends('master')
#section('blog')
<div class="span12 well">
{{ link_to_route('photos.index', 'Back to index') }}
</div>
<div class="span12 well">
{{ Form::open(array('route' => 'photos.store', 'files'=> true)) }}
{{ Form::label('title', 'Title: ') }}
{{ Form::text('title') }}
{{ Form::label('caption', 'Caption: ') }}
{{ Form::textarea('caption') }}
{{ Form::label('image', 'Image: ') }}
{{ Form::file('image') }}
<br>
{{ Form::submit('Add Photo', array('class' => 'btn btn-primary' )) }}
{{ Form::close() }}
<br><br>
#if($errors->any())
{{ implode('', $errors->all('<li class="error">:message</li>')) }}
#endif
</div>
#stop
you can use Form::file('image');
see http://laravel.com/docs/html#file-input
{{ Form::open(array('action' => 'HomeController#upload', 'files'=>true)) }}
{{ Form::label('image', 'Upload Image')}}
{{ Form::file('image') }}
{{ Form::submit('Submit') }}
{{ Form::close() }}
Step2:
public function upload(){
$image = Input::file('image');
$savepath = 'public/uploads/';
$filename = $image->getClientOriginalName();
Input::file('image')->move($savepath, $filename);
}
Final Step:
Route::get('/upload' function()
{
return View::make('upload');
});
Route::post('/upload', 'HomeController#upload');