CodeIgniter致命错误:调用未定义的函数

时间:2013-12-02 21:17:53

标签: php codeigniter

我正在使用CodeIgniter。 我正在重用图书馆上传文件到谷歌驱动器。现在我面临着错误。

  

致命错误:调用未定义的函数upLoadGoogleDriveFile()   d:\ XAMPP \ htdocs中\ butfix \脑预告思特\应用\控制器\ blabla.php   在第21行

调用堆栈

Time    Memory  Function    Location

1   0.0499  146680     {main}( )    ..\index.php:0
2   0.0916  183128     require_once( 'D:\xampp\htdocs\butfix\Brain-Teaser-Fest\system\core\CodeIgniter.php' )   ..\index.php:202
3   1.2976  3225664    call_user_func_array ( ) ..\CodeIgniter.php:359
4   1.2976  3225712    Blabla->index( ) ..\CodeIgniter.php:359

现在我的控制器类是: blabla.php

<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Blabla extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('MY_DriveServiceHelper');
        $this->load->helper('file');
    }

    public function index() {

        echo "Welcome to Bulls' warmed leavings";

        $path = "butfix" . DIRECTORY_SEPARATOR . "bookstore" . DIRECTORY_SEPARATOR . "filesystem" . DIRECTORY_SEPARATOR . "source" . DIRECTORY_SEPARATOR . "original.jpg";

        upLoadGoogleDriveFile($path);
    }

}

?>

我的图书馆班级是: MY_DriveServiceHelper.php

<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

class MY_DriveServiceHelper {

    const BACKUP_FOLDER = 'PHPBackups';
    const SHARE_WITH_GOOGLE_EMAIL = 'my_email@gmail.com';
    const CLIENT_ID = '111111.apps.googleusercontent.com';
    const SERVICE_ACCOUNT_NAME = '123asdf@developer.gserviceaccount.com';
    const KEY_PATH = "asdf-privatekey.p12";

    protected $scope = array('https://www.googleapis.com/auth/drive');
    private $_service;

    public function __construct() {

        $client = new Google_Client();
        $client->setClientId(self::CLIENT_ID);

        $inFile = NULL;
        if (file_exists(self::KEY_PATH)) {
            $inFile = read_file(self::KEY_PATH);
        }
        $client->setAssertionCredentials(new Google_AssertionCredentials(
                self::SERVICE_ACCOUNT_NAME, $this->scope, $inFile)
        );

        $this->_service = new Google_DriveService($client);
    }

    public function __get($name) {
        return $this->_service[$name];
    }

    public function createFile($name, $mime, $description, $content,
            Google_ParentReference $fileParent = null) {
        $file = new Google_DriveFile();
        $file->setTitle($name);
        $file->setDescription($description);
        $file->setMimeType($mime);

        if ($fileParent) {
            $file->setParents(array($fileParent));
        }

        $createdFile = $this->_service->files->insert($file,
                array(
            'data' => $content,
            'mimeType' => $mime,
        ));

        return $createdFile['id'];
    }

    public function createFileFromPath($path, $description,
            Google_ParentReference $fileParent = null) {
        $fi = new finfo(FILEINFO_MIME);
        $mimeType = explode(';', $fi->buffer(read_file($path)));
        $fileName = preg_replace('/.*\//', '', $path);

        return $this->createFile($fileName, $mimeType[0], $description,
                        read_file($path), $fileParent);
    }

    public function createFolder($name) {
        return $this->createFile($name, 'application/vnd.google-apps.folder',
                        null, null);
    }

    public function setPermissions($fileId, $value, $role = 'writer',
            $type = 'user') {
        $perm = new Google_Permission();
        $perm->setValue($value);
        $perm->setType($type);
        $perm->setRole($role);

        $this->_service->permissions->insert($fileId, $perm);
    }

    public function getFileIdByName($name) {
        $files = $this->_service->files->listFiles();
        foreach ($files['items'] as $item) {
            if ($item['title'] == $name) {
                return $item['id'];
            }
        }

        return false;
    }

    public function upLoadGoogleDriveFile($path) {

        printf("Uploading %s to Google Drive\n", $path);

        $folderId = $this->getFileIdByName(BACKUP_FOLDER);

        if (!$folderId) {
            echo "Creating folder...\n";
            $folderId = $this->createFolder(BACKUP_FOLDER);
            $this->setPermissions($folderId, SHARE_WITH_GOOGLE_EMAIL);
        }

        $fileParent = new Google_ParentReference();
        $fileParent->setId($folderId);

        $fileId = $this->createFileFromPath($path, $path, $fileParent);

        printf("File: %s created\n", $fileId);

        $this->setPermissions($fileId, SHARE_WITH_GOOGLE_EMAIL);
    }

}

3 个答案:

答案 0 :(得分:0)

您应该创建一个MY_DriveServiceHelper的实例,您可以在其上调用该方法:

$helper = new MY_DriveServiceHelper();
$helper->upLoadGoogleDriveFile($path);

答案 1 :(得分:0)

upLoadGoogleDriveFile()是MY_DriveServiceHelper的类函数。您将需要实例化MY_DriveServiceHelper,然后调用它:

$MY_DriveServiceHelper->upLoadGoogleDriveFile();

查看一些OOP basics

答案 2 :(得分:0)

您的MY_DriveServiceHelper实际上是一个图书馆。这意味着当您执行new MY_DriveServiceHelper()时,CodeIgniter会调用$this->load->library('MY_DriveServiceHelper');。然后将对象存储在$this->DriveServiceHelper

所以,你需要做$this->DriveServiceHelper->upLoadGoogleDriveFile()

DOCS:http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html