2011-08-12 50 views
1

如何創建上傳圖片功能?一旦圖像上傳並且用戶訪問主頁,圖像將使用cakephp框架顯示在類似facebook的主頁上?使用cakephp 1.3上傳圖片

有沒有一個我可以遵循或引導的例子?

謝謝。

回答

0

只需在你的控制器的add()函數中添加這樣的內容即可。

// File functions 
    if ($this->data['MODELNAME']['FILENAME']) { 
     $file = new File($this->data['MODELNAME']['FILENAME']['tmp_name']); 
     $ext = $this->data['MODELNAME']['FILENAME']['name']; 
     $point = strrpos($ext,'.'); 
     $ext = substr($ext,$point+1,(strlen($ext)-$point)); 

     /*if ($ext != 'jpg' && $ext != 'jpeg' && $ext != 'gif' && $ext != 'png') { 
      $this->Session->setFlash('You may only upload image files.'); 
      $this->render(); 
     } else {*/ 
     $filename = 'filename.'.$ext; 
     $data = $file->read(); 
     $file->close(); 
     $file = new File(WWW_ROOT.'/img/'.$filename,true); 
     $file->write($data); 
     $file->close(); 
     //} 
    } 
0

您在提問之前沒有檢查麪包房或谷歌?

Anyway try this out(第一個結果在谷歌搜索我)