我有一个方法用于在会话中存储文件并在目录中上传,之后我从会话中获取数据并保存在数据库中。
我对此方法有疑问。有时它的工作正确,但有些情况下并非会话中都保存了所有项目。
我使用Laravel Session::put()
方法将单独的项目保存到会话数组中。每次用户更改表单上的某些输入类型文件时,都会调用此方法。
例如,我有3个输入类型文件。有时,当我尝试上传文件时,它会保存信息front_passport
字段和address_document
字段,但它不会保存back_passport
字段。
Laravel会话是否有某种情况并以某种方式使用它?
我的代码如下:
$(".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'));
}
});
});
&#13;
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()]
));
}
&#13;
我的HTML是:
<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>
&#13;
答案 0 :(得分:0)
laravel会话有一个生命周期,你可以从app / session.php配置它。但这不是这种情况 - 当你删除一个文件时,如果它已经存在于目录中。 我认为您不需要会话才能找到现有文件,只需使用php file_exists函数
即可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]]
));
}