tags:

views:

127

answers:

2
<?php
class UploadsController extends AppController {
var $name = 'Uploads';
var $components = array('Auth');
var $uses = array('Upload');

function beforeFilter() {
    $this->Auth->allow('*');
}

function upload($event) {
    App::import('Vendor', 'UploadedFiles', array('file' => 'UploadedFiles.php'));

    $user = $this->Auth->user('id');
    $this->set('user', $user);
    if(!$this->Auth->user()) { $this->Session->setFlash(__('Please login.', true)); }
    echo $user;
    echo $event;

    $vardir = date('d-m-Y');
    $dir = 'img/gallery/'.$vardir.'/';
    $thmbdir = 'img/gallery/'.$vardir.'/thumbnails/';    
    if(!is_dir($dir)) {
        mkdir($dir, 0777);
        mkdir($thmbdir, 0777);
    }
    $galleryPath = $dir;

    $absGalleryPath = realpath($galleryPath) . '/';
    $absThumbnailsPath = realpath($galleryPath . 'thumbnails/') . '/';

    //Iterate through uploaded data and save the original file, thumbnail, and description.
    while(($file = UploadedFiles::fetchNext()) !== null) {
      $fileName = $file->getSourceFile()->getSafeFileName($absGalleryPath);
      $file->getSourceFile()->save($absGalleryPath . '/' . $fileName);

      $thumbFileName = $file->getThumbnail(1)->getSafeFileName($absThumbnailsPath);
      $file->getThumbnail(1)->save($absThumbnailsPath . '/' . $thumbFileName);

        $this->Upload->create();
        $this->Upload->set(array(
            'name' => $absGalleryPath . $fileName,
            'width' => $file->getSourceFile()->getWidth(),
            'height' => $file->getSourceFile()->getHeight(),
            'description' => $file->getDescription(),
            'event_id' => $event,
            'user_id' => $user
          ));
        $this->Upload->save();
    }
  }
}

Check the last part of the code where I try to save to the database. It doesn't save because $event and $users become empty. But when I echo them (line 17 and 18), they do appear on the page with correct values. It seems that they are being 'erased' in the while loop... If I put some dummy data for 'event_id' => '123' and 'user_id' => '123', the scripts saves successfully. But if I use the variables, it doesn't save. What happens with my variables? Any idea?

Thanks

A: 

I'm guessing there's an error in your model. Try creating the array first, and printing it, and see if it creates the array correctly. Then make sure your model matches the array, ie. that the field types match the field types you input. '123' isn't the same as 123, since '123' is a string, and 123 is an integer.

Tor Valamo
You can check this hypothesis by casting the values to string: `event_id' => (string)$event, user_id' => (string)$user`
Ewan Todd
Hi Tor,There is no error in the model array. Both '123' and 123 are being saved. I just don't understand how the $event variable can be cleared?
octavian
Hi Ewan, I tried and it inserts a blank field. Again, meaning $event is empty... but when I echo it on line 17 it's 11042...
octavian
When casting $event as integer, it appears 0. So the variable is really empty... What a mystery.
octavian
A: 

i can think of a couple things i'd try though it's not to say you haven't already.

  • echo $user and $event the line just before and just after the while loop, to verify the problematic line.

  • set the variables to another temp variable:

$t_user = $user;

$t_event = $event;

then try using the $t_ variables in the loop.

Adam