On Thu, Oct 7, 2010 at 4:43 PM, Deek <[email protected]> wrote:
> In order to get around this issue of having multiple models using the
> FileUpload.FileUpload behavior I did the following:
>
> created two additional behavior files:
>
> private_file_upload.php and public_file_upload.php in /app/plugins/
> file_upload/models/behaviors
>
> containing the following code respectively:
>
> <?php
>
> App::Import('Behavior','FileUpload.file_upload');
>
> class PrivateFileUploadBehavior extends FileUploadBehavior {
>
> }
> ?>
>
> <?php
>
> App::Import('Behavior','FileUpload.file_upload');
>
> class PublicFileUploadBehavior extends FileUploadBehavior {
>
> }
> ?>
>
> then in the model configurations I changed
>
> var $actsAs = array(
>       'FileUpload.FileUpload'
>
> to
>
> var $actsAs = array(
>       'FileUpload.PrivateFileUpload'
>
> and
>
> var $actsAs = array(
>       'FileUpload.PublicFileUpload'
>
>
> This has allowed me to use two separate configurations for each upload
> type, you can probably use this same method, just create separate
> behavior files to extend the base FileUpload for each different
> configuration you want to have.

I'm using my own modified version of the plugin, and for the same
reason. Some files need to be above webroot and some not.. And many
should be in separate directories based on User.id and so on, so I
need to change directories on the fly. But my approach was to edit the
component, as well as  plugins/file_upload/vendors/uploader.php

In the component:

 /**
    * base path to append upload dir to
    *
    * @var string
    * @access public
    */
var $base_path = WWW_ROOT;

Then, all instances of WWW_ROOT should be changed to $this->base_path.

New method:

/**
 * change upload directory path
 *
 * @param       string  $path   the new upload path
 * @return      void
 * @access      public
 */
public function changeDir($path)
{
        $this->options['uploadDir'] = $path;
        $this->Uploader->options['uploadDir'] = $this->base_path . $path;
}


In uploader.php, processFile() method:

//make sure the file doesn't already exist, if it does, add an itteration to it
$up_dir = $this->options['uploadDir'];

/* added by me
*/
if (!is_dir($up_dir))
{
        if (!mkdir($up_dir, 0775, true))
        {
                $this->_error('Uploader::processFile() - Unable to create 
directory:
' . $up_dir);
                return false;
        }
}


I then set model to null in the config. My controllers have a
protected method _handleUpload() which is called when the component
detcts an upload:

GalleriesController::addImage

if (!empty($this->data))
{       
        if ($this->FileUpload->hasFile)
        {
                if ($image_id = $this->_handleUpload())
                {
                        $this->flash(
                                'Image uploaded successfully',
                                array('controller' => 'galleries', 'action' => 
'view', 'id' => $gallery_id)
                        );
                }
        }
        else if (sizeof($this->FileUpload->errors))
        {
                $this->set('errors', $this->FileUpload->errors);
        }

}


protected function _handleUpload()
{
        /* clean file names up a bit
         */
        $file_var = $this->FileUpload->options['fileVar'];

        foreach($this->FileUpload->uploadedFiles as $key => $file)
        {
                $ext = strrchr($this->FileUpload->uploadedFiles[$key]['name'], 
'.');
                $name = 
basename($this->FileUpload->uploadedFiles[$key]['name'], $ext);
                $this->FileUpload->uploadedFiles[$key]['name'] =
Inflector::slug(strtolower($name)) . strtolower($ext);
        }
        
        /* first set $this->FileUpload->base_path to APP if required to be
above WWW_ROOT
         */
        $this->FileUpload->changeDir('files/galleries/' .
$this->data['Gallery']['member_id']);

        /* move the files to proper directory
         */
        $this->FileUpload->processAllFiles();
        
        if ($this->FileUpload->success)
        {
                foreach($this->FileUpload->finalFiles as $k => $v)
                {
                        $path = $this->FileUpload->options['uploadDir'] . DS .
$this->FileUpload->finalFiles[$k];
                        
                        // re-size image, create thumbnails, save info to db ...
                }
        }

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to