所以我有这个图像上传功能,我可以上传图像并更新数据库上的图像路径。问题是图像没有上传,也没有在数据库上更新。所有其他记录都已成功更新,但不是图像。请帮忙。
以下是我的代码
edit.ctp
<form id="form">
<img src="<?php echo $this->base ?>/uploads/employees/{{ data.Employee.image }}" width="110" height="100" class="user-image photo">
<ul class="list-group w110px">
<span class="btn btn-primary btn-block btn-sm btn-file">
Choose File
<input id="employeeImage" onchange="readURL(this)" name="picture" class="form-control" type="file" accept="image/gif, image/jpeg, image/png">
</span>
</ul>
</div>
<button class="btn btn-primary btn-sm btn-block" ng-click="update()"><i class="fa fa-save"></i> SAVE</button>
</form>
controller.js
app.controller('EmployeeEditController', function($scope, $routeParams, Employee, Select) {
$('#form').validationEngine('attach');
$scope.employeeId = $routeParams.id;
// load employee
$scope.load = function() {
Employee.get({ id: $scope.employeeId }, function(e) {
$scope.data = e.data;
});
// department selection
Select.get({ code: 'departments' }, function(e) {
$scope.departments = e.data;
});
Select.get({ code: 'positions' }, function(e) {
$scope.positions = e.data;
});
// employee-types selection
Select.get({ code: 'employee-types' }, function(e) {
$scope.types = e.data;
});
}
$scope.load();
// update employee
$scope.update = function() {
valid = $("#form").validationEngine('validate');
if (valid) {
Employee.update({ id: $scope.employeeId } ,$scope.data, function(e) {
if (e.ok) {
$.gritter.add({
title: 'Successful!',
text: e.msg
});
window.location = '#/employees';
}else {
$.gritter.add({
title: 'Warning!',
text: e.msg
});
}
});
}
}
});
api.php
public function edit($id = null) {
$employeeImage = @$_FILES['picture']['name'];
if (!empty($employeeImage)) {
$attachmentExt = pathinfo($employeeImage, PATHINFO_EXTENSION);
$this->request->data['Employee']['image'] = $this->Employee->nextId() . '.' . $attachmentExt;
}
if($this->Employee->save($this->request->data)) {
$response = array(
'ok' => true,
'msg' => 'Employee has been added.',
'data' => $this->request->data['Employee']
);
} else {
$response = array(
'ok' => false,
'msg' => 'Employee cannot be saved this time.'
);
}
if (!empty($employeeImage)) {
$path = "uploads/employees";
if(!file_exists('uploads')) mkdir('uploads');
if(!file_exists('uploads/employees')) mkdir('uploads/employees');
if(!file_exists($path)) mkdir($path);
move_uploaded_file($_FILES['attachment']['tmp_name'], $path . '/' . $this->request->data['Employee']['image']);
}
$this->set(array(
'response' => $response,
'_serialize' => 'response'
));
}
答案 0 :(得分:0)
你可以尝试一下
public function add() {
if ($this->request->is('Post')) {
$this->Post->create();
$filename = WWW_ROOT. DS . 'upload'.DS.$this->data['Post']['doc_file']['name'];
move_uploaded_file($this->data['Post']['doc_file']['tmp_name'],$filename);
$this->request->data['Post']['image'] = $this->data['Post']['doc_file']['name'];
unset( $this->request->data['Post']['doc_file'] );
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('Saved successfully',true), 'default', array('class' => ' success'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('Not saved. Please try again',true), 'default', array('class' => 'failer '));
}
}
}