【问题标题】:Find function is missing, but not sure what to put in缺少查找功能,但不确定要输入什么
【发布时间】:2013-11-22 21:05:22
【问题描述】:

好的,我正在尝试此处指定的文件和文件夹类:

http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#

我的文件夹控制器如下所示:

<?php
App::uses('AppController', 'Controller');
App::uses('Folder', 'Utility');
App::uses('File', 'Utility');

class FolderController extends AppController {

    public $uses = array();

    public function display() {

      $dir = '/var/www/html/letters/pdfdir/';
      $files = $dir->find('.*\.pdf', true);

      foreach ($files as $file) {
        $file = new File($dir->pwd() . DS . $file);
        $contents = $file->read();

        echo 'CONTENT: '.$contents;

        // $file->write('I am overwriting the contents of this file');
        // $file->append('I am adding to the bottom of this file.');
        // $file->delete(); // I am deleting this file
        $file->close(); // Be sure to close the file when you're done
      }

    }

}

但我收到以下错误:

错误:在非对象上调用成员函数 find() 文件:/var/www/html/letters/app/Controller/FolderController.php 线路:13

任何帮助表示赞赏。

问候

【问题讨论】:

    标签: cakephp


    【解决方案1】:

    你错了

    $dir = '/var/www/html/letters/pdfdir/';
    

    应该是

     $dir = new Folder('/var/www/html/letters/pdfdir');
    

    在您的原始代码中,$dir 只是一个字符串类型的变量。但是,它应该是 Folder 类的对象。

    另一个注意事项:您必须删除尾部斜杠。

    【讨论】:

    • 我已经手动创建了文件夹,我不想创建一个新的。这就是我改变那条线的原因。
    • 谢谢,我才意识到这一点。现在更改代码,看看会发生什么。
    【解决方案2】:

    $dir 仍然是一个字符串,您需要将它传递给 File 或 Folder 对象才能用作对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      • 2010-09-26
      • 2014-11-22
      • 1970-01-01
      • 1970-01-01
      • 2020-09-19
      • 1970-01-01
      相关资源
      最近更新 更多