tags:

views:

110

answers:

2

This works:

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

      echo $directory;
      $this->_saveUploadedFiles('C:/xampp/htdocs/freetickets/app/webroot/img/gallery/zantje_11042/');
    }

  function _saveUploadedFiles($galleryPath) {

    $absGalleryPath = $galleryPath;
    $absThumbnailsPath = 'C:\xampp\htdocs\freetickets\app\webroot\img\gallery\zantje_11042\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);
    }
  }

But this doesn't:

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

      echo $directory; //echoes C:/xampp/htdocs/freetickets/app/webroot/img/gallery/zantje_11042/
      $this->_saveUploadedFiles($directory);
    }

  function _saveUploadedFiles($galleryPath) {

    $absGalleryPath = $galleryPath;
    $absThumbnailsPath = 'C:\xampp\htdocs\freetickets\app\webroot\img\gallery\zantje_11042\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);
    }
  }

Only difference is the $this->_saveUploadedFiles('C:/xampp/htdocs/freetickets/app/webroot/img/gallery/zantje_11042/') and one with a variable as path: $this->_saveUploadedFiles($directory);

So why doesn't it work when using $directory, even if $directory echoes C:/xampp/htdocs/freetickets/app/webroot/img/gallery/zantje_11042/ ?

Thanks

A: 

Check the way you have put / and \ as folder separators in paths.

Manjula
Or he can use the DS constant.
Wayne Khan
A: 

It seems it isn't a problem related to directories but to the value of the variables dissapearing...

    <?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

octavian