2015-10-07 194 views
0

所以我有這個圖片上傳功能,我可以上傳圖片並更新數據庫上的圖片路徑。問題是圖像不能在數據庫上傳和更新。所有其他記錄已成功更新,但不是圖像。請幫忙。CakePHP更新圖片上傳

下面是我的代碼

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

什麼是您的上傳文件的文件大小?你有沒有檢查上傳文件大小是否不超過你的php.ini設置'upload_max_filesize'和'post_max_size'?你有什麼試圖調試上傳?日誌文件中的任何錯誤消息? –

+0

嗨,日誌文件沒有錯誤..我上傳的文件大小小於兆字節..當我運行scipt時沒有錯誤顯示..它顯示成功,但上傳文件夾中沒有文件.. –

回答

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 ')); 
     } 
     } 
    }