Welcome Guest! You need to login or register to make posts.

Notification

Icon
Error

Options
Go to last post Go to first unread
Tanya  
#1 Posted : Wednesday, November 18, 2009 1:04:00 PM(UTC)
Tanya

Rank: Advanced Member

Groups: Member
Joined: 5/14/2007(UTC)
Posts: 24

Was thanked: 1 time(s) in 1 post(s)
Let's consider a simple project which adds Image Uploader to a page within CakePHP framework.

First of all, get CakePHP.

Then, according to CakePHP concept, add Model, Controller, and View for our application:

Model (/app/models/upload.php)

Code:
<?php
class Upload extends AppModel{
    var $name = 'Upload';
}
?>


Controller (/app/controllers/uploads_controller.php)

Code:
<?
class UploadsController extends AppController {
    var $name = 'Uploads';
	
    function index(){

    }
}
?>


View (/app/views/uploads/index.ctr)

Code:
<?
App::import('Vendor', 'ImageUploaderClass', array('file' => 'ImageUploader.class.php'));
$imageUploader = new ImageUploader("ImageUploader1", 650, 400);
$imageUploader->setScriptsDirectory("js/Scripts/");
$imageUploader->render();
?>


To get it worked copy the ImageUploaderPHP folder to /vendors/ folder and Scripts to /app/webroot/js/.

Do not forget to bind the Controller with the '/' path (in /app/config/routes.php):

Code:
Router::connect('/', array('controller' => 'uploads', 'action' => 'index'));


The attached zip contains a pure CakePHP with Image Uploader inside.
File Attachment(s):
cakePHP.zip (3,746kb) downloaded 169 time(s).
Best regards,
Tatyana Bertyakova

UserPostedImage Follow Aurigma on Twitter!
Octavian86  
#2 Posted : Thursday, November 19, 2009 4:54:39 AM(UTC)
Octavian86

Rank: Newbie

Groups: Member
Joined: 11/17/2009(UTC)
Posts: 3

Thanks :)

The $imageUploader->setScriptsDirectory("js/Scripts/"); made it work.
andreym  
#3 Posted : Monday, December 7, 2009 1:04:54 PM(UTC)
andreym

Rank: Advanced Member

Groups: Member
Joined: 6/16/2009(UTC)
Posts: 134

Was thanked: 8 time(s) in 8 post(s)
This sample based on the previous. The upload functionality has been added. It uploads an image with thumbnail and shows them after uploading.

The uploads controller has the new action upload which contains the upload logic. And new gallery controller and view provide display functionality.

Edited by user Monday, December 7, 2009 1:45:32 PM(UTC)  | Reason: Not specified

File Attachment(s):
cake_php.zip (3,734kb) downloaded 65 time(s).
Octavian86  
#4 Posted : Saturday, December 19, 2009 3:50:22 AM(UTC)
Octavian86

Rank: Newbie

Groups: Member
Joined: 11/17/2009(UTC)
Posts: 3

It seems that the image uploader doesn't accept my variables...

Code:

<?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. 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. How can I save the variables?
andreym  
#5 Posted : Monday, December 21, 2009 12:49:57 PM(UTC)
andreym

Rank: Advanced Member

Groups: Member
Joined: 6/16/2009(UTC)
Posts: 134

Was thanked: 8 time(s) in 8 post(s)
Hello!

I've tried to save to the database and it works fine for me.
This is the table shema I used:
Code:

CREATE TABLE `cake_php`.`uploads` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`width` INT NULL ,
`height` INT NULL ,
`description` VARCHAR( 512 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`event_id` INT NOT NULL ,
`user_id` INT NOT NULL
) ENGINE = MYISAM ;


And this is the code I used to save data:
Code:

<?php

class UploadsController extends AppController {
  var $name = 'Uploads';

  /**
   * This controller does not use a model
   */
  var $uses = array('Upload');

  function index() {

  }

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

    //This variable specifies relative path to the folder, where the gallery with uploaded files is located.
    //Do not forget about the slash in the end of the folder name.
    $galleryPath = 'img/gallery/';

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

    $event = 123;
    $user = 321;

    //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();
    }
  }
}


It sets the variables value before loop and saves successfully. So I assume your problem isn't that the variables erased in the while loop.
Octavian86  
#6 Posted : Tuesday, December 22, 2009 5:35:06 PM(UTC)
Octavian86

Rank: Newbie

Groups: Member
Joined: 11/17/2009(UTC)
Posts: 3

No, you set the variables as constants in the script. The $event variable must come the url (GET): function upload($event) { }!

Just look at my script. The changes you made, work but don't do what it's supposed to do. You set $event = 123, pointless. If $event was a constant, I would set 'event_id' => 123 in the Upload array. But $event is passed from the url (= so it's value can change).

Anyway, I figured out why the variables get erased. I had to send them with the photo upload form POST and not through cakes GET.

The only problem I now have is that my login session (Auth) is erased after I upload files d'oh!.
andreym  
#7 Posted : Tuesday, December 22, 2009 9:38:54 PM(UTC)
andreym

Rank: Advanced Member

Groups: Member
Joined: 6/16/2009(UTC)
Posts: 134

Was thanked: 8 time(s) in 8 post(s)
You need to disable Session.checkAgent option, since Image Uploader send his own User Agent value while uploading files. It can be disabled in the app/config/core.php file.
And the second, disable the PreserveHttpCookies property of Image Uploader. Add the
Code:
$imageUploader->setPreserveHttpCookies(false);
line before
Code:
$imageUploader->render();
in the app/views/uploads/index.ctp file.
After these two changes login session should work.

Edited by user Tuesday, December 22, 2009 10:05:23 PM(UTC)  | Reason: Not specified

Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.