I have a method that I use to store files in session and upload in directory, after that I get data from session and save in database.
I have issue with this method. Sometimes it works correct but there are cases when not all items have been saved in session.
I'm using Laravel Session::put()
method to save separate items into session array. Every time user changes some input type file on the form, this method is invoked.
For example, I have 3 input type files. Sometimes when I try to upload files, it saves ins ession front_passport
field and address_document
field but it doesn't save back_passport
field.
Is there some case with Laravel session and using it in some way?
My code is the following:
$(".app-file").on('change', function(){
var formData = new FormData();
formData.append($(this).attr('name'), this.files[0]);
formData.append('name', $(this).attr('name'));
$.ajax({
url: store_files,
type: "POST",
data: formData,
contentType: false,
processData: false,
success: function(response) {
if(response.errors) {
$.each(response.errors, function (k, v) {
$('[name=' + k + ']').parent().find('.file-name').html('');
$('<label class="error file_error">' + v + '</label>').insertAfter($('[name=' + k + ']').next().next());
});
}
},
error: function () {
$('.ajax-error').remove();
$('<span class="error col-md-6 ajax-error">There is some error! Please try again later!</span>').insertAfter($('.upload-section h3'));
}
});
});
public function storeFiles() {
$name = Input::get('name'); //input name
$input = Input::all();
$input_file = Input::file($name);
$current_time = time();
$allowed = array('jpeg','jpg','gif','png','pdf');
$file_name = $current_time . '_' . $input_file->getClientOriginalName();
$extension = File::extension($file_name);
if (!in_array($extension, $allowed)) {
return Response::json(array(
'success' => false,
'errors' => [$name => 'Please provide an image with one of the following extensions : jpg, gif, png or pdf']
));
}
$current_session = Session::get($name); //delete existing image from directory
$item_path = APPLICATIONS_DIR.'/'.$current_session;
if (file_exists($item_path) && !empty($current_session)) {
$removed_file = unlink($item_path);
}
if ($input_file) {
$input_file->move(APPLICATIONS_DIR, $file_name);
}
Session::put($name, $file_name);
return Response::json(array(
'files' => ['name' => $name,'session' => Session::all()]
));
}
My html is:
<div class="form-group">
<label for="front_passport" class="col-md-2 label-file">{{translate("Front ID/Passport")}}<span class="required-label">*</span></label>
<div class="col-md-8 file-wrapper">
{{ Form::file('front_passport', ["class"=>"app-file"]) }}
<label for="front_passport" class="file-upload"><span>{{translate("Upload a File")}}</span></label>
<div class="file-name" data-input="front_passport"></div>
<label for="front_passport" generated="true" class="<?php echo $errors->has('front_passport') ? 'error' : '';?>">{{ $errors->first('front_passport') }}</label>
</div>
</div>
<div class="form-group">
<label for="back_passport" class="col-md-2 label-file">{{translate("Back ID/Passport")}}<span class="required-label">*</span></label>
<div class="col-md-8 file-wrapper">
{{ Form::file('back_passport', ["class"=>"app-file"]) }}
<label for="back_passport" class="file-upload"><span>{{translate("Upload a File")}}</span></label>
<div class="file-name" data-input="back_passport"></div>
<label for="back_passport" generated="true" class="<?php echo $errors->has('back_passport') ? 'error' : '';?>">{{ $errors->first('back_passport') }}</label>
</div>
</div>
<div class="form-group">
<label for="address_document" class="col-md-2 label-file">{{translate("Proof of Address")}}<span class="required-label">*</span></label>
<div class="col-md-8 file-wrapper">
{{ Form::file('address_document', ["class"=>"app-file"]) }}
<label for="address_document" class="file-upload"><span>{{translate("Upload a File")}}</span></label>
<div class="file-name" data-input="address_document"></div>
<label for="address_document" generated="true" class="<?php echo $errors->has('address_document') ? 'error' : '';?>">{{ $errors->first('address_document') }}</label>
</div>
</div>
</div>
The laravel session has a lifetime, you can configure it from app/session.php. But that is no the case here - when you delete a file if it is already exist in the directory. I don't think you need a session inorder to find the existing file, simply use php file_exists function
public function storeFiles() {
$name = Input::get('name'); //input name
$input = Input::all();
$input_file = Input::file($name);
$current_time = time();
$allowed = array('jpeg', 'jpg', 'gif', 'png', 'pdf');
$file_name = $current_time . '_' . $input_file->getClientOriginalName();
$extension = File::extension($file_name);
if (!in_array($extension, $allowed)) {
return Response::json(array(
'success' => false,
'errors' => [$name => 'Please provide an image with one of the following extensions : jpg, gif, png or pdf']
));
}
//delete existing image from directory
$item_path = APPLICATIONS_DIR . '/' . $file_name;
if (file_exists($item_path)) {
$removed_file = unlink($item_path);
}
if ($input_file) {
$input_file->move(APPLICATIONS_DIR, $file_name);
}
return Response::json(array(
'files' => ['name' => $name, 'session' => [$name => $file_name]]
));
}