2013-02-28 34 views
1

像我有下面的代碼上傳文件中的CakePHP上傳和檢索CakePHP的

class UsersController extends AppController { 

    var $name = 'Users'; 

    function index() { 
     $this->set('users', $this->User->find('all')); 
    } 

    function add() { 
     if (!empty($this->data)) { 
      if ($this->User->save($this->data)) { 
       $this->Session->setFlash('Your user data has been saved.'); 
       $this->redirect(array('action' => 'index')); 
      } 
      $this->User->create(); 
      if ($this->uploads() && $this->User->save($this->data)) { 
       $this->Session->setFlash(_('Upload saved', true)); 
      } else { 
       $this->Session->setFlash(_('Upload not saved.Please try again', true)); 
      } 
     } 
    } 

    function uploads() { 
     $uploads_dir = '/uploads'; 
     $users = $this->request->data['Upload']['file']; 
     if ($users['error'] === UPLOAD_ERR_OK) { 
      if (move_uploaded_file($users['User']['file'], $uploads_dir)) { 
       $this->User->saveAll($this->data); 
       return true; 
      } 
     } 
     return false; 
    } 

    function edit($id = null) { 
     $this->User->id = $id; 
     if (empty($this->data)) { 
      $this->data = $this->User->read(); 
     } else { 
      if ($this->User->save($this->data)) { 
       $this->Session->setFlash('Your user details has been updated.'); 
       $this->redirect(array('action' => 'index')); 
      } 
     } 
    } 

    function delete($id) { 
     $this->User->delete($id); 
     $this->Session->setFlash('This user has been deleted.'); 
     $this->redirect(array('action' => 'index')); 
     } 
} 

當我試圖上傳,文件被uplaoding但我需要上傳的文件中,當超級鏈接觀看給每個.jpeg顯示,但我得到的錯誤爲

「請求的地址'/Users/app/webroot/index.php/uploads'在此服務器上找不到」。

也請幫助我如何上傳圖像存儲在另一個文件夾

PS ::代碼應該純粹的B CakePHP的

請幫幫忙, 在此先感謝

回答

1

確保使用的文件夾上傳文件有足夠的權限,您可以使用Cakephp媒體視圖來下載文件。爲了您的參考,請檢查。 Downloading files using media view in CakePHP

在函數下載中使用圖像名稱或圖像ID作爲參數,並將路徑更改爲 'path'=> APP。 '上傳'。 DS

「uploads」文件夾應位於webroot目錄內。

+0

謝謝你的幫助@ammu – user2110147 2013-03-01 04:42:07