0

I am using CodeIgniter. I am reusing a Library for upload file to google drive. Now I am facing error.

Fatal error: Call to undefined function upLoadGoogleDriveFile() in D:\xampp\htdocs\butfix\Brain-Teaser-Fest\application\controllers\blabla.php on line 21

Call Stack

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

Now my Controller Class is : 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);
    }

}

?>

And my Library Class is : 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 = '[email protected]';
    const CLIENT_ID = '111111.apps.googleusercontent.com';
    const SERVICE_ACCOUNT_NAME = '[email protected]';
    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 Answers 3

1

You should create an instance of MY_DriveServiceHelper on which you call the method:

$helper = new MY_DriveServiceHelper();
$helper->upLoadGoogleDriveFile($path);
Sign up to request clarification or add additional context in comments.

1 Comment

I'd recommend using CI's loader to load the helper. $this->load->helper('your_helper');
1

Your MY_DriveServiceHelper is actually a library. Which means CodeIgniter will call new MY_DriveServiceHelper() when you do $this->load->library('MY_DriveServiceHelper');. The object is then stored at $this->DriveServiceHelper.

So, you need to do $this->DriveServiceHelper->upLoadGoogleDriveFile().

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

Comments

0

upLoadGoogleDriveFile() is a class function of MY_DriveServiceHelper. You will need to instantiate MY_DriveServiceHelper and then call it like so:

$MY_DriveServiceHelper->upLoadGoogleDriveFile();

Have a look at some of the OOP basics.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.