Cakephp FileUpload Plugin - Данни за сесията - Персонализиран път на директория

В момента разработвам в cakephp 2.3, работещ в среда на php5.

Успях да изтегля и внедря Jquery File Upload (https://github.com/hugodias/FileUpload ), който е базиран на оригиналния дизайн на blueimp (https://github.com/blueimp/jQuery-File-Upload).

Изглежда всичко работи правилно. Трябва обаче динамично да променя директорията за качване въз основа на данните за влезлите потребители. Компонентът за качване е както следва:

class UploadComponent extends Component
{   
protected $options;

/*
* $options = array()
* Avaliable Options:
*
* 
* $options => array(
*   'upload_dir' => 'files/{your-new-upload-dir}' // Default 'files/'
* )
*/
function __construct( ComponentCollection $collection, $options = null ) {

    $this->UploadModel = ClassRegistry::init('FileUpload.Upload');

    $this->options = array(
        'script_url' => Router::url('/', true).'file_upload/handler',
        'upload_dir' => WWW_ROOT.'files/',
        'upload_url' => $this->getFullUrl().'/files/',
        'param_name' => 'files',
        // Set the following option to 'POST', if your server does not support
        // DELETE requests. This is a parameter sent to the client:
        'delete_type' => 'DELETE',
        // The php.ini settings upload_max_filesize and post_max_size
        // take precedence over the following max_file_size setting:
        'max_file_size' => null,
        'min_file_size' => 1,
        'accept_file_types' => '/.+$/i', // For only accept images use this: ([^\s]+(\.(?i)(jpg|png|gif|bmp))$)
        'max_number_of_files' => null,
        // Set the following option to false to enable resumable uploads:
        'discard_aborted_uploads' => true,
        // Set to true to rotate images based on EXIF meta data, if available:
        'orient_image' => false,
        'image_versions' => array()
    );

    # Check if exists new options
    if( $options )
    {
        # Change the upload dir. If it doesn't exists, create it.
        if( $options['upload_dir'] )
        {

            // Remove the first `/` if it exists.
            if( $options['upload_dir'][0] == '/' )
            {
                $options['upload_dir'] = substr($options['upload_dir'], 1);
            }


            $dir = WWW_ROOT.$options['upload_dir'];

            // Create the directory if doesn't exists.
            if( !file_exists( $dir) )
            {
                @mkdir( $dir );
            } 

            $this->options['upload_url'] = $this->getFullUrl().'/'.$dir;
            $this->options['upload_dir'] = $dir;
        }
    }

}

Сега трябва да променя upload_dir, за да добавя въз основа на идентификатора на влезлите потребители. Нещо като:

'upload_dir' => WWW_ROOT.'files/'.$this->Session->read('Auth.User.id').'/',

Опитах се да декларирам var $components = array('Session') в UploadComponent, но без успех.

Също така съм сигурен, че директорията за създаване не работи, тъй като не създава файл, ако кодирам твърдо upload_dir.

Аз съм начинаещ в cakephp, така че може да са пропуснати очевидни стъпки.

За разбирането,

Cloud_R


person Cloud_Ratha    schedule 30.04.2013    source източник


Отговори (1)


Хюго успя да ми изпрати имейл за поддръжка:

$this->options = array(
        'script_url' => Router::url('/', true).'file_upload/handler',
        'upload_dir' => WWW_ROOT.'files/'.AuthComponent::user('id').'/',
        'upload_url' => $this->getFullUrl().'/files/'.AuthComponent::user('id').'/',
        ...

Това автоматично разреши проблема.

person Cloud_Ratha    schedule 02.05.2013