如何使用cakephp上傳文件?有沒有任何框架支持文件上傳,或者我應該寫我自己的代碼呢?在cakephp中上傳文件
回答
編輯(2015年):請參閱Awesome CakePHP列表當前文件的插件(2.x的分支here)
原來的答覆:
CakePHP的上傳在積極開發插件(如十月的2010):
- 大衛·佩爾鬆的Media Plugin [slides]
- WebTechNick的CakePHP File Upload Handling Plugin [blog post]
- 萬里約翰遜Uploader Plugin [website]
- 韋Código's MeioUpload 2.0 Behavior Plugin [website]
您也可以使用File class,但我不會在這一個上重新發明輪子。
此組件可以幫助您:http://cakeforge.org/snippet/detail.php?type=snippet&id=36。 允許使用FTP上傳到數據庫或目錄。我有一些CakePHP的經驗,但是我還沒有嘗試過這個組件。
Cakeforge不再使用(在2008年底通過thechaw取代,然後在2009年年底的GitHub),只保留現場提供的舊代碼存檔。這個特定的組件是爲CakePHP 1.2編寫的,並且自1.3穩定以來並沒有更新。 – deizel 2010-10-12 12:35:11
既可以
對於初學者來說這可能是更好的選擇: http://www.milesj.me/resources/script/uploader-plugin
要開始嘗試這個。
我花了兩天的時間尋找上傳文件的簡單方法,我嘗試了很多方法,無法使用任何工具。這工作。這是不安全的,它是超級基礎。對我來說,現在是一個跳板。我會用這個來理解過程。然後你就可以在複雜的環境中構建它。
對我來說,我努力嘗試保存$this->data
- 但因爲它不像cakePHP博客教程。你想要的數據(所有的文件信息)在嵌套數組中被埋入幾層,所以$this->data['Doc']['files']
就是你所追求的。
SQL
CREATE TABLE IF NOT EXISTS `docs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(300) NOT NULL,
`type` varchar(300) NOT NULL,
`tmp_name` varchar(300) NOT NULL,
`error` tinyint(1) NOT NULL,
`size` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
模型
<?php
class Doc extends AppModel {
}
?>
VIEW
<h1>Uploads</h1>
<table>
<tr>
<th>ID</th><th>File Name</th><th>Size</th>
</tr>
<?php foreach($files as $file): ?>
<tr>
<td><?php echo $file['Doc']['id'];?></td>
<td><?php echo $this->Html->link($file['Doc']['name'],array('controller' => 'files','action'=>'uploads',$file['Doc']['name']));?></td>
<td><?php echo number_format($file['Doc']['size']/1023,0).' KB';?></td>
</tr>
<?php endforeach;?>
</table>
<h1>Add a File</h1>
<?php
echo $this->Form->create('Doc',array('type'=>'file'));
echo $this->Form->file('File');
echo $this->Form->submit('Upload');
echo $this->Form->end();
?>
CON TROLLER
<?php
class DocsController extends AppController
{
public $helpers = array('Html','Form','Session');
public function index()
{
// -- list the files -- //
$this->set('files',$this->Doc->find('all'));
// -- Check for error -> Upload file to folder -> Add line to database -- //
if($this->request->is('post'))
{
if($this->data['Doc']['File']['error']=='0')
{
if(file_exists('files/uploads/' . $this->data['Doc']['File']['name']))
{
$this->Session->setFlash('A file called ' .$this->data['Doc']['File']['name']. ' already exists');
} else {
move_uploaded_file($this->data['Doc']['File']['tmp_name'], 'files/uploads/' . $this->data['Doc']['File']['name']);
}
$this->Doc->save($this->data['Doc']['File']);
$this->redirect(array('action'=>'index'));
}
}
}
}
?>
- 1. Cakephp文件上傳問題
- 2. CakePHP 2.0文件上傳
- 3. Cakephp多文件上傳
- 4. 驗證文件上傳CakePHP
- 5. Cakephp 3 jquery文件上傳
- 6. CakePHP的圖像/文件上傳插件
- 7. CakePHP的文件上傳問題
- 8. CakePHP的文件上傳驗證器
- 9. CakePHP + Jquery文件上傳教程
- 10. 的CakePHP和PHP文件上傳混亂
- 11. CakePHP&MeioUpload - WMV文件不上傳
- 12. CakePHP的檢索文件數據上傳
- 13. CakePHP 2.0文件上傳不起作用
- 14. 增加上傳文件大小cakephp 3
- 15. CakePHP/phpunit:如何模擬文件上傳
- 16. cakePHP可選文件上傳驗證
- 17. CakePHP的上傳文件選項沒有得到我米試圖上傳XLSX文件在CakePHP名
- 18. 在Cakephp中上傳AJAX文件輸入圖像
- 19. 如何在cakephp中下載上傳文件時檢查會話?
- 20. 如何在CakePHP中執行基於表單的文件上傳?
- 21. CakePHP的形式傳上傳文件通過Ajax控制器
- 22. 在FileNet中上傳文件?
- 23. 在CodeIgniter中上傳文件
- 24. 在asp.net中上傳文件?
- 25. 在ASP.NET中上傳文件
- 26. 在WatiN中上傳文件
- 27. 在asp.net中上傳文件
- 28. 在Rails中上傳文件
- 29. 在Silverlight中上傳文件
- 30. 在Codeigniter中上傳文件
相關:http://stackoverflow.com/questions/3148203/best-practice-to-upload-files-in-cakephp/3162144#3162144 – deizel 2010-10-12 12:22:51
你錯過了https://github.com/burzum/cakephp-file -storage – burzum 2015-08-04 08:12:16
謝謝@burzum,我已經指出了現在維護的列表的兩個答案:) – deizel 2015-08-04 19:29:20