【问题标题】:file upload in cakephp在 cakephp 中上传文件
【发布时间】:2011-04-24 05:36:51
【问题描述】:

如何使用 cakephp 上传文件?是否有任何框架支持文件上传或者我应该为此编写自己的代码?

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    这个组件可以帮助你:http://cakeforge.org/snippet/detail.php?type=snippet&id=36。 允许使用 FTP 上传到数据库或目录。我有一些使用 CakePHP 的经验,但是我还没有尝试过这个组件。

    【讨论】:

    • Cakeforge 已不再使用(在 2008 年底被 thechaw 取代,然后在 2009 年底被 github 取代),仅保留提供旧代码存档。这个特定的组件是为 CakePHP 1.2 编写的,自从 1.3 稳定后就没有更新过。
    【解决方案2】:

    两个都可以

    对于初学者来说,这可能是更好的选择: http://www.milesj.me/resources/script/uploader-plugin

    【讨论】:

      【解决方案3】:

      编辑(2015):请参阅Awesome CakePHP 列表以获取当前文件插件(2.x 分支here


      原答案:

      CakePHP 上传插件正在积极开发中(截至 2010 年 10 月):

      你也可以使用File class,但我不会在这个上重新发明轮子。

      【讨论】:

      【解决方案4】:

      要开始尝试这个。

      我花了两天时间寻找一种简单的上传文件的方法,我尝试了很多方法,但都无法奏效。这行得通。它不安全,它是超级基本的。对我来说,它现在是一个跳板。我会用它来理解这些过程。然后你就可以把它复杂化了。

      对我来说,我一直在努力保存$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 {
          }
      ?>
      

      查看

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

      控制器

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

      【讨论】:

        猜你喜欢
        • 2011-01-14
        • 1970-01-01
        • 2012-09-24
        • 2015-07-08
        • 2012-08-15
        • 2014-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多