PHP code example of netcommons / files

1. Go to this page and download the library: Download netcommons/files library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

netcommons / files example snippets


public $components = array(
    'Files.Download',
    'AuthorizationKeys.AuthorizationKey' => [
        'operationType' => 'redirect',
        'targetAction' => 'download_pdf',
        'model' => 'BlogEntry',
    ],
);

public function download_pdf() {
    // ここから元コンテンツを取得する処理
    $this->_prepare();
    $key = $this->params['pass'][1];

    $conditions = $this->BlogEntry->getConditions(
            Current::read('Block.id'),
            $this->Auth->user('id'),
            $this->_getPermission(),
            $this->_getCurrentDateTime()
    );

    $conditions['BlogEntry.key'] = $key;
    $options = array(
            'conditions' => $conditions,
            'recursive' => 1,
    );
    $blogEntry = $this->BlogEntry->find('first', $options);
    // ここまで元コンテンツを取得する処理

    // 認証キーによるガード
    $this->AuthorizationKey->guard('redirect', 'BlogEntry', $blogEntry);

    // ダウンロード実行
    if ($blogEntry) {
        return $this->Download->doDownload($blogEntry['BlogEntry']['id'], ['filed' => 'pdf']);
    } else {
        // 表示できない記事へのアクセスなら404
        throw new NotFoundException(__('Invalid blog entry'));
    }
}


public $components = array(
    'Files.Download',
    'AuthorizationKeys.AuthorizationKey' => [
        'operationType' => 'redirect',
        'targetAction' => 'download_pdf',
        'model' => 'BlogEntry',
    ],
);

    $this->AuthorizationKey->guard('popup', 'BlogEntry', $blogEntry);

<div>
    PDF :
     echo $this->Html->link('PDF',
            '#',
        ['authorization-keys-popup-link',
            'url' => $this->NetCommonsHtml->url(
                [
                    'action' => 'download_pdf',
                    'key' => $blogEntry['BlogEntry']['key'],
                    'pdf',
                ]
            ),
            'frame-id' => Current::read('Frame.id')
        ]
    );