Upload videos in Laravel project - php

I am having trouble when I try to upload video/movie to my site. It doesn't save the video in the database, but it makes a folder 'movie' in my files with that video in it like it's supposed to. Also I edited my php.ini file for size requirements and session that I made says that it uploaded. Here is my code
View:
<div class="col-md-6">
{!! Form::open(['method'=>'POST', 'action'=> 'MovieController#store', 'files' => true]) !!}
<div class="form-group">
{!! Form::label('movie_name', 'Enter Movie Name:') !!} <br>
{!! Form::text('movie_name', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('uploaded_path', 'Select Movie:') !!} <br>
{!! Form::file('uploaded_path', null, ['class'=>'form-control'])!!}
</div>
</div>
<div class="form-group">
{!! Form::label('actor_id', 'Actors:') !!}
{!! Form::select('actor_id[]', $actors, null, ['class'=>'form-control js-example-basic-multiple', 'multiple' => 'multiple']) !!}
</div>
<div class="form-group">
{!! Form::label('category_id', 'Category:') !!}
{!! Form::select('category_id[]', $categories, null, ['class'=>'form-control js-example-basic-multiple', 'multiple' => 'multiple']) !!}
</div>
MovieRequest:
public function rules()
{
return [
'movie_name' => 'required|max:255',
'uploaded_path' => 'mimetypes:video/avi,video/mpeg,video/mp4|required'
];
}
Controller:
public function store(MovieRequest $request)
{
DB::beginTransaction();
try {
if ($request->hasFile('uploaded_path')) {
$filenameWithExt = $request->file('uploaded_path')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('uploaded_path')->getClientOriginalExtension();
$fileNameToStore = $filename. '_'.time().'.'.$extension;
$path = $request->file('uploaded_path')->storeAs('public/movies/', $fileNameToStore);
} else {
$fileNameToStore = 'novideo.mp4';
}
$movie = new Movie;
$movie->movie_name = $request->input('movie_name');
$movie->uploaded_path = $fileNameToStore;
$movie->actors()->attach($request->input('actor_id'));
$movie->categories()->attach($request->input('category_id'));
$movie->save();
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
}
Session::flash('success', 'A movie was successfully UPLOADED in the database!');
return redirect()->route('movies.index');
}

Don’t forget to add:
enctype="multipart/form-data"
to movie field.

Related

Update image won't work

I'm trying to update some data with image. The other data is updated but the image still not updated. here's my code
route
Route::get('film/{idFilm}/edit', array('as' => 'film.edit', 'uses' => 'FilmController#edit'));
Route::post('film/{idFilm}/update', array('as' => 'film.update', 'uses' => 'FilmController#update'));
controller
public function edit($idFilm)
{
$film = Film::findOrFail($idFilm);
$genre = Genre::lists('namaGenre', 'idGenre');
if (is_null($film))
{
return Redirect::to('film');
}
return View::make('pengelolaan.film.editfilm', compact('film','genre'));
}
/**
* Update the specified resource in storage.
*
* #param int $id
* #return Response
*/
public function update($idFilm)
{
$rules = array(
'judulFilm' => 'required',
'durasi' => 'required|numeric',
'keterangan' => 'required',
'idGenre' => 'required'
);
$validation = Validator::make(Input::all(), $rules);
if ($validation->fails())
{
return Redirect::to('film/' . $idFilm . '/edit')
->withErrors($validation)
->withInput()
->with('message', 'There were validation errors.');
}
else
{
$films = Film::find($idFilm);
$films->judulFilm=Input::get('judulFilm');
$films->durasi=Input::get('durasi');
$films->keterangan= Input::get('keterangan');
$films->idGenre= Input::get('idGenre');
if(Input::hasFile('foto'))
{
$file=Input::file('foto');
$file->move('img',$file->getClientOriginalName());
$filename=$file->getClientOriginalName();
$films->foto = $filename;
$films->save();
}
else
{
$films->save();
}
Session::flash('message', 'Data Berhasil Diubah');
return Redirect::to('film');
}
}
view
{{Form::model($film, array('route'=>array('film.update', $film->idFilm,'files' => TRUE)))}}
<div class="form-group">
<div class="col-lg-6">
{{ Form::label('judulFilm', 'Judul Film') }}
{{ Form::text('judulFilm', Input::old('judulFilm'), array('class' => 'form-control')) }}
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
{{ Form::label('durasi', 'Durasi Film') }}
{{ Form::text('durasi', Input::old('durasi'), array('class' => 'form-control')) }}
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
{{ Form::label('keterangan', 'Sinopsis Film') }}
{{ Form::textarea('keterangan', Input::old('keterangan'), array('class' => 'form-control')) }}
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
{{ Form::label('idGenre', 'Genre') }}
{{ Form::select('idGenre', $genre,'',array('class'=>'form-control')) }}
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
{{ Form::label('foto', 'Poster') }}
{{ Form::file('foto') }}
</div>
</div>
</br>
<div class="form-group">
<div class="col-lg-6">
<a class="btn btn-default " href="{{ url('film') }}">Batal</a>
{{Form::submit('Simpan', array('type'=>'submit', 'class'=>'btn btn-default'))}}
{{Form::close()}}
There's no error so i don't know whats wrong with it.
Can someone please tell me what's wrong? Thanks in advance
There are few possibilities to face this issue :
Check for have enctype="multipart/form-data" attribute
Check your file size. If your file size is too high increase upload_max_filesize
May be permission dined for your tmp folder. Provide permission for your tem folder. ini_get('upload_tmp_dir');

Laravel post form error

I'm having a problem for the first time when i submit a form.
When i submit the form it doesn't go to post route and i don't know why.
my post route is that:
Route::post('/app/add-new-db', function()
{
$rules = array(
'name' => 'required|min:4',
'file' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
// get the error messages from the validator
$messages = $validator->messages();
// redirect our user back to the form with the errors from the validator
return Redirect::to('app/add-new-db')
->withErrors($validator);
}
else
{
$name = Input::get('name');
$fname = pathinfo(Input::file('file')->getClientOriginalName(), PATHINFO_FILENAME);
$fext = Input::file('file')->getClientOriginalExtension();
echo $fname.$fext;
//Input::file('file')->move(base_path() . '/public/dbs/', $fname.$fext);
/*
DB::connection('mysql')->insert('insert into databases (name, logotipo) values (?, ?)',
[$name, $fname.$fext]);
*/
//return Redirect::to('app/settings/');
}
});
And my html:
<div class="mdl-cell mdl-cell--12-col content">
{!! Form::open(['url'=>'app/add-new-db', 'files'=>true]) !!}
<div class="mdl-grid no-verticall">
<div class="mdl-cell mdl-cell--12-col">
<h4>Adicionar Base de Dados</h4>
<div class="divider"></div>
</div>
<div class="mdl-cell mdl-cell--6-col">
<div class="form-group">
{!! Form::label('name', 'Nome: ') !!}
{!! Form::text('name', null, ['id'=> 'name', 'class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('image', 'Logotipo:') !!}
{!! Form::file('image', ['class'=>'form-control']) !!}
#if ($errors->has('image'))
<span class="error">{{ $errors->first('image') }}</span>
#endIf
</div>
<div class="form-group">
{!! Form::submit('Adicionar', ['id'=>'add-new-db', 'class'=>'btn btn-default']) !!}
<p class="text-success"><?php echo Session::get('success'); ?></p>
</div>
</div>
</div>
{!! Form::close() !!}
</div>
I'm loading this from from a jquery get:
function addDB()
{
$( "#settings-new-db" ).click(function(e)
{
$.get('/app/add-new-db', function(response)
{
$('.content-settings').html("");
$('.content-settings').html(response);
componentHandler.upgradeDom();
});
e.preventDefault();
});
}
When i try to submit the form i'm getting 302 found in network console from chrome.
I'm doing forms at this way and it is happens for the first time. Anyone can help me?
Thanks
Fix the name attribute for your image upload field. You refer it as image in the form but you are trying to fetch it as file in your controller.

Call to undefined method Illuminate\Http\Request::angel.jpg()

I have a form, I have troubles in Upload an Image :(
I am trying to upload some image and I don't know what I am doing bad :(
{{ Form::open (['route' => 'titulos.store', 'class'=> 'form', 'files'=> 'true']) }}
{{ Form::label('title', "Titulo:", ['class' => 'col-sm-2 control-label']) }}
{{ Form::text('title') }}
{{ $errors->first('title') }}
<div class="form-group">
{{ Form::label('date', "Fecha:", ['class' => 'col-sm-2 control-label']) }}
<input type="date" name="date" >
</div>
{{ Form::label('description', "Description:", ['class' => 'col-sm-2 control-label']) }}
{{ Form::textarea('description') }}
{{ $errors->first('description') }}
<div class="form-group">
{{ Form::file('image') }}
</div>
{{ Form::label('category_id', 'Category:', ['class' => 'col-sm-2 control-label']) }}
<div class="col-sm-10">
{{ Form::select('category_id', array('1' => 'TBLeaks', '2' => 'Quejas', '3' => 'Denuncias', '4' => 'Ideas'), null, array('class' => 'form-control')) }}
</div>
<div class="row">
<div class="col-sm-offset-2 col-sm-10">
{{ Form::submit('Submit', ['class' => "btn btn-primary"]) }}
</div>
</div>
<div class="row">
<div class="col-sm-offset-2 col-sm-10">
<a class="btn btn-success" href="{{ URL::to('admin') }}">Back to Admin</a>
</div>
</div>
{{ Form::close() }}
</div>
#if(Session::has('message'))
<div class="alert alert-{{ Session::get('class') }}">{{ Session::get('message')}}</div>
#endif
#stop
In my store function I have:
class TitulosController extends BaseController {
public function store(){
$rules = array(
'title' => 'required',
'description' => 'required',
'category_id' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
// proceso de valicion
if ($validator->fails()) {
return Redirect::to('titulos/create')
->withErrors($validator)
->withInput()->withErrors($validator);
} else {
//store
$image = Input::file('image');
$filename = $image->getClientOriginalName();
if(Input::hasFile('image')){
Input::file('image')->move(public_path().'/assets/img/', $filename);
}
$titulo = new Titulo();
$titulo->id = Input::get('id');
$titulo->title = Input::get('title');
$titulo->description = Input::get('description');
$titulo->date = Input::get('date');
$titulo->image = Input::$filename('image');
$titulo->category_id = Input::get('category_id');
$titulo->save();
return Redirect::to('titulos');
}
I have this model for the Titulo table:
class Titulo extends Eloquent {
use SoftDeletingTrait; // for soft delete
protected $dates = ['deleted_at']; // for soft delete
protected $table = 'titulos';
protected $primaryKey = 'id';
protected $fillable = array('image');
public $timestamps = true;
public function __construct() {
parent::__construct();
}
public function category(){
return $this->belongsTo('Category');
}
}
I have this for Image model:
class Image extends Eloquent {
public $timestamps = false;
protected $fillable = array('image');
}
At this line where your store to public path should be no problem
if(Input::hasFile('image')){
Input::file('image')->move(public_path().'/assets/img/', $filename);
}
Only your save Tutilo object looks not good. I think you have mistakenly add $ on Input::filename at this line. which will call the image
$titulo->image = Input::$filename('image');
change your code to this
$titulo = new Titulo();
$titulo->id = Input::get('id');
$titulo->title = Input::get('title');
$titulo->description = Input::get('description');
$titulo->date = Input::get('date');
$titulo->image = $filename; // I change this line. assuming you want to store the file name
$titulo->category_id = Input::get('category_id');

Dropzone shows upload success but file is not uploaded in Laravel 5

I'm still learning Laravel and trying to build some sort of an article management system. As part of form for inserting new articles I want to also upload multiple pictures of them. The upload shows success, yet there is no file in uploads folder. Here is my form code:
{!! Form::open(['route' => 'admin_artikli.store', 'class' => 'dropzone', 'id' => 'create_artikal', 'enctype' => 'multipart/form-data' ]) !!}
<div class="form-group">
{!! Form::label('Firma', 'Firma') !!}
{!! Form::text('firma_id', Input::old('firma_id'), array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('Naziv', 'Naziv') !!}
{!! Form::text('naziv', Input::old('naziv'), array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('Opis', 'Opis') !!}
{!! Form::text('opis', Input::old('opis'), array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('Cijena', 'Cijena') !!}
{!! Form::text('cijena', Input::old('cijena'), array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('kolicina', 'kolicina') !!}
{!! Form::text('kolicina', Input::old('kolicina'), array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('dostupnost', 'dostupnost') !!}
{!! Form::text('dostupnost', Input::old('dostupnost'), array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::select('aktivan', array('D' => 'Aktivan', 'N' => 'Neaktivan'), 'D') !!}
</div>
<div class="form-group">
{!! Form::select('aktivan', array('D' => 'Aktivan', 'N' => 'Neaktivan'), 'D') !!}
</div>
{!! Form::submit('Kreiraj artikal', array('class' => 'btn btn-primary', 'id' => 'create_artikal_submit')) !!}
{!! Form::close() !!}
My routes.php(showing only relevant):
Route::post('/upload', 'ArtikalAdminController#upload');
Route::resource('admin_artikli', 'ArtikalAdminController');
My controller methods for store and upload:
public function upload(){
$input = Input::all();
$rules = array(
'file' => 'image|max:3000'
);
echo "eldaaaaaaaaaaaaaaaaaaaaaaaaar";
$validation = Validator::make($input, $rules);
if ($validation->fails())
{
return Response::make($validation->errors->first(), 400);
}
$file = Input::file('file');
$extension = File::extension($file['name']);
$directory = public_path().'uploads/'.sha1(time());
$filename = sha1(time().time()).".{$extension}";
$file->move($directory, $filename);
$upload_success = Input::file('file', $directory, $filename)->move($directory, $filename);;
echo $directory;
if( $upload_success ) {
return Response::json('success', 200);
} else {
return Response::json('error', 400);
}
}
public function store(Request $request)
{
$artikal = new Artikal;
$artikal->firma_id = Input::get('firma_id');
$artikal->naziv = Input::get('naziv');
$artikal->sifra = Input::get('sifra');
$artikal->opis = Input::get('opis');
$artikal->cijena = Input::get('cijena');
$artikal->kolicina = Input::get('kolicina');
$artikal->dostupnost = Input::get('dostupnost');
$artikal->aktivan = Input::get('aktivan');
$artikal->save();
Session::flash('flash_message', 'Uspješno unesen artikal!');
//return redirect()->back();
}
Finally, dropzone options I am currently using:
Dropzone.options.createArtikal = { // The camelized version of the ID of the form element
// The configuration we've talked about above
autoProcessQueue: false,
uploadMultiple: true,
parallelUploads: 10,
maxFiles: 10,
// The setting up of the dropzone
init: function() {
var myDropzone = this;
// First change the button to actually tell Dropzone to process the queue.
var element = document.getElementById('create_artikal_submit');
var form = document.getElementById('create_artikal');
element.addEventListener("click", function(e) {
// Make sure that the form isn't actually being sent.
e.preventDefault();
e.stopPropagation();
myDropzone.processQueue();
});
// Listen to the sendingmultiple event. In this case, it's the sendingmultiple event instead
// of the sending event because uploadMultiple is set to true.
this.on("sendingmultiple", function() {
// Gets triggered when the form is actually being sent.
// Hide the success button or the complete form.
});
this.on("successmultiple", function(files, response) {
// Gets triggered when the files have successfully been sent.
// Redirect user or notify of success.
form.submit();
});
this.on("errormultiple", function(files, response) {
// Gets triggered when there was an error sending the files.
// Maybe show form again, and notify user of error
});
}
}
I know this is lacking many things, but I'm trying to go on step at a time and I'm stuck here. When I check the network tab of dev tools I see XHR requests are 200 OK, but yet I see no file.
You forgot to write 'files' => true in form, update form & try.
{!! Form::open(['route' => 'admin_artikli.store', 'class' => 'dropzone', 'id' => 'create_artikal', 'files'=>true, 'enctype' => 'multipart/form-data' ]) !!}

Laravel change password right after email confirmation view not working

When a user is created a random password (and a auth code) will be created and send with an email to the user.
When clicked on the link in the email the user status will be 1 (so active) and the user will be able to change his password right away.
Now it doesn't work as I want to.
UserController:
public function store(CreateUserRequest $request, User $user, Attribute $attribute)
// some unnecessary code
if ((Input::get('usertype_id')) > 1) {
$randomPassword = str_random(8);
$user->password = Hash::make($randomPassword);
$authentication_code = str_random(12);
$user->authentication_code = $authentication_code;
$user->active = 0;
};
$user->save();
if ((Input::get('usertype_id')) > 1) {
// Email sturen met verficatie code
$email = Input::get('email');
Mail::send('emails.user', ['user' => $user, 'password' => $randomPassword, 'authentication_code' => $authentication_code], function ($message) use ($email) {
$message->to($email, 'Lilopel')->subject('Lilopel: Verify your account!');
});
};
public function confirmUser($authentication_code)
{
if (!$authentication_code)
{
return 'auth code not found!';
}
$user = User::where('authentication_code', '=', $authentication_code)->first();
if (!$user)
{
return 'user not found!';
}
$user->active = 1;
$user->save();
Session::put('user_id', $user->id);
return view('user.setpassword', ['user' => $user]);
//return redirect()->route('user.setPassword', [$user_id]);
}
public function setPassword(SetPasswordRequest $request)
{
$user_id = Session::get('user_id');
$user = $this->user->find($user_id);
$user->fill($request->only('password'));
$user->save();
}
Route:
Route::get('user/verify/{authenticationCode}', 'UserController#confirmUser');
Route::get('user/password', 'UserController#setPassword');
View:
{!! Form::model($user, ["route"=>['user.setPassword', $user->id] , "method" => 'PATCH']) !!}
<div class="form-group {{ $errors->has('password') ? 'has-error' : '' }}">
{!! Form::label('password', trans('common.password'), ['class' => 'form-label col-sm-3 control-label
text-capitalize']) !!}
<div class="col-sm-6">
{!! Form::password('password', ['name' => 'password', "class"=>"form-control","placeholder" =>
trans('common.password') ]) !!}
{!! $errors->first('password', '<span class="help-block">:message</span>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('confirm_password') ? 'has-error' : '' }}">
{!! Form::label('password_confirmation', trans('common.confirmpassword'), ['class' => 'form-label
col-sm-3 control-label text-capitalize']) !!}
<div class="col-sm-6">
{!! Form::password('password_confirmation', ['name' => 'password_confirmation',
"class"=>"form-control","placeholder" => trans('common.confirmpassword') ]) !!}
{!! $errors->first('password_confirmation', '<span class="help-block">:message</span>') !!}
</div>
</div>
{!! Form::submit( trans('common.edit'), ["class"=>"btn btn-primary text-capitalize center-block "]) !!}
{!! Form::close() !!}
Email links works, the status of the user gets active, but then the .blade will give a Route [user.setPassword] not defined. (View: public_html/server2/resources/views/user/setpassword.blade.php) error.
work togetherTo use the route as you do, you need a named route.
Change this
Route::get('user/password', 'UserController#setPassword');
to this
Route::get('user/password', [
'as' => 'user.setPassword',
'uses' => 'UserController#showProfile'
]);
Also, make sure the HTTP verbs of the route and your form's method work together.

Categories