Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading Variables CakePhp (assistance Needed)

Posted on 2010-09-03
12
Medium Priority
?
345 Views
Last Modified: 2013-11-10
Hi All,

I have a component which contains a variable and I need to read it from my controller.

Here is the code:

<?php
class FileUploadComponent extends Object {
      var $components = array('RequestHandler');
      
      function upload($data) {
            
            
            $folder = '203jfjg546650f0gf405764455887';
            
            mkdir md5($folder);
            
            
            
            $destination = 'img/uploads/'.md5($folder).'/'.$data['name'];
            if(move_uploaded_file($data['tmp_name'], $destination)) {
                  
                  return true;
            }  
            return false;
      }
}
?>

Can anyone help please?

Thanks
0
Comment
Question by:error77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 10

Expert Comment

by:NetExpert-Warszawa
ID: 33599027
Which variable? $destination?

Make it a class property. See the code
class FileUploadComponent extends Object {
  //...

  protected $destination;

  public function upload($data) {
    //...
    $this->destination = 'img/uploads/'.md5($folder).'/'.$data['name'];
    //...
  }

  public function getDestination() {
    return $this->destination;
  }
}

// to access the variable:

$component = new FileUploadComponent();
$component->upload(...);
$variable = $component->getDestination();

Open in new window

0
 

Author Comment

by:error77
ID: 33600562
Hi NetExpert... Couldn't get it to work but it's propably me ... I might have done something wrong.

Here is the actual component code.

Could you help me sort the code out so the variables can be accessed please?

Here is the code in full:

/***********************************/
* component
/***********************************/
<?php
class FileUploadComponent extends Object {
      var $components = array('RequestHandler');
      

      function upload($data) {
            
            
            $folder = '1223322';
            $tempdest  = 'img/uploads/';
            $random = rand(1111111111, 9999999999);
            
            mkdir($tempdest.''.$random.''.md5($folder));
            
            $fullpath = $tempdest.''.$random.''.md5($folder);
            
            
            $this->Cookie->write('rfname',$fullpath);
            
            $destination = 'img/uploads/'.$random.''.md5($folder).'/'.$data['name'];
            if(move_uploaded_file($data['tmp_name'], $destination)) {
                  
                  return true;
            }  
            return false;
      }
}
?>

Hope you can help.

Thanks

0
 

Author Comment

by:error77
ID: 33600971
Anyone please?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 10

Expert Comment

by:NetExpert-Warszawa
ID: 33602329
Tell me which variables you want to access please.

In general, declare each variable, you want to access, in the classes scope instead of in functions scope. If you declare them with public modifier (or without any), you will be able to access them in a $component->variable way.
0
 

Author Comment

by:error77
ID: 33602334
Folder and destination please.
0
 
LVL 10

Accepted Solution

by:
NetExpert-Warszawa earned 2000 total points
ID: 33602346
getDestination() and getFolder() will return the variables you want.
/***********************************/
* component
/***********************************/
<?php
class FileUploadComponent extends Object {
      var $components = array('RequestHandler');
      protected $folder;
      protected $destination;
      
      function upload($data) {            
            
            $this->folder = '1223322';
            $tempdest  = 'img/uploads/';
            $random = rand(1111111111, 9999999999);
            
            $fullpath = $tempdest.$random.md5($this->folder);
            mkdir($fullpath);                        
            
            $this->Cookie->write('rfname',$fullpath);
            
            $this->destination = $fullpath.'/'.$data['name'];
            if(move_uploaded_file($data['tmp_name'], $this->destination)) {
                  
                  return true;
            }  
            return false;
      }

  public function getDestination() {
    return $this->destination;
  }

  public function getFolder() {
    return $this->folder;
  }
}
?>

Open in new window

0
 

Author Comment

by:error77
ID: 33602368
Can you give me a usage example so I can try it please?
0
 

Author Comment

by:error77
ID: 33602450
I've tried using this on the controller:

$component = new FileUploadComponent();
$test = $component->getFolder();
                  echo $test;

It shows no errors but no results either.

I'm I doing it wrong?
0
 
LVL 10

Expert Comment

by:NetExpert-Warszawa
ID: 33602496
Notice that variables receive their values in upload method.
$component = new FileUploadComponent();
$component->upload(array('name'=>'blablabla'));
$test = $component->getFolder();
echo $test;

Open in new window

0
 

Author Comment

by:error77
ID: 33602615
It's perfect ... just 1 thing ... I'm getting an error .. seems like somthings broken in the component?

Notice (8): Undefined index:  tmp_name [APP/controllers/components/file_upload.php, line 21]

Any ideas pls?
0
 
LVL 10

Expert Comment

by:NetExpert-Warszawa
ID: 33602896
Line 21 is:
$this->destination = $fullpath.'/'.$data['name'];
and 22:
if(move_uploaded_file($data['tmp_name'], $this->destination)) {

As you can see $data is an array and needs to have at least two indexes 'name' and 'tmp_name'.
$component = new FileUploadComponent();
$component->upload(array('name'=>'blablabla', 'tmp_name'=>'tmp_blabla'));
$test = $component->getFolder();
echo $test;

Open in new window

0
 

Author Closing Comment

by:error77
ID: 33603187
Perfect! Thank you very much for your help!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question