1

How can I upload a file using cakephp? is there any framework support for file uploading or should I write my own code for this ?

4 Answers 4

5

Edit (2015): Please see the Awesome CakePHP list for current file plugins (2.x branch here)


Original answer:

CakePHP upload plugins in active development (as of Oct 2010):

You could also use the File class, but I wouldn't reinvent the wheel on this one.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @burzum, I've pointed both answers to the maintained list now :)
1

both is possible

for beginners this is probably the better choice: http://www.milesj.me/resources/script/uploader-plugin

Comments

0

This component could help you out: http://cakeforge.org/snippet/detail.php?type=snippet&id=36. Allows uploads to either a database or a directory using FTP. I have some experience with CakePHP, however I have not yet tried this component.

1 Comment

Cakeforge is no longer in use (superseded by thechaw at the end of 2008, then github at the end of 2009) and only remains live to provide an archive of old code. This particular component was written for CakePHP 1.2, and hasn't been updated since 1.3 became stable.
0

To just get going try this.

I spent two days searching for a simple way to upload files, I tried lots of methods and couldn't get any to work. This works. It is not secure, it is super basic. For me it is now a springboard. I would use this to understand the processes. Then you can build it up in complexity.

For me I struggled with trying to save $this->data - but of cause it is not like the cakePHP blog tutorial. The data you want (all the file info) is buried a couple of levels down in nested arrays so $this->data['Doc']['files'] is what you are after.

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 ; 

MODEL

<?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(); ?> 

CONTROLLER

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.