0

I want to use another variable from another function,

I am tried to use the global variable, but I faild.

class UploadController extends Controller
{
    public $file;// declare my $file varable

    function actionIndex()
    {
        $dir = Yii::getPathOfAlias('application.uploads');

        $uploaded = false;
        $model=new Upload();

        if(isset($_POST['Upload']))
        {
            $model->attributes=$_POST['Upload'];
            global $file; //use the $file varable

            $file=CUploadedFile::getInstance($model,'file');
                 //get a instance into $file varable.

            if($model->validate()){
                $uploaded = $file->saveAs($dir.'/'.$file->getName());
            }
        }
        $this->render('index', array(
                'model' => $model,
                'uploaded' => $uploaded,
                'dir' => $dir,
        ));
    }

    public function actionDownload(){

        var_dump($file);exit; //here printed out a null

        $path = Yii::getPathOfAlias('/yiiroot/trackstar/protected/uploads/')."23602414.pdf";

        $upload=new Upload();

        $upload->downloadFile($path);
    }
}

I declared a $file varable and assigned an instance into it in the functin actionIndex. Then, I want to use the $file varable at function actionDownload in order to print out the return value of $file->getname();

But, I got a null when I "var_dump($file);exit;" in function actionDownload. Could you help me?

I tried to change my code like:

function actionIndex() {

        $this->file=CUploadedFile::getInstance($model,'file');

        ...
}

But, in "actionDownload()".

public function actionDownload(){

    var_dump($this);exit;

... }

When I "var_dump($this);exit;" as above, I could't get the value of 'file'. It printed out as below:

E:\projects\yiiroot\trackstar\protected\controllers\UploadController.php:37:
object(UploadController)[10]
  public 'file' => null
  public 'layout' => string '//layouts/column1' (length=17)
  public 'menu' => 
    array (size=0)
      empty
  public 'breadcrumbs' => 
    array (size=0)
      empty
  public 'defaultAction' => string 'index' (length=5)
  private '_id' (CController) => string 'upload' (length=6)
  private '_action' (CController) => 
    object(CInlineAction)[11]
      private '_id' (CAction) => string 'download' (length=8)
      private '_controller' (CAction) => 
        &object(UploadController)[10]
      private '_e' (CComponent) => null
      private '_m' (CComponent) => null
  private '_pageTitle' (CController) => null
  private '_cachingStack' (CController) => null
  private '_clips' (CController) => null
  private '_dynamicOutput' (CController) => null
  private '_pageStates' (CController) => null
  private '_module' (CController) => null
  private '_widgetStack' (CBaseController) => 
    array (size=0)
      empty
  private '_e' (CComponent) => null
  private '_m' (CComponent) => null

Could you help me.

George
  • 47
  • 8
  • Why don't you use `$this` keyword? – Zain Farooq Dec 21 '18 at 12:32
  • I solved this problem according below tipic. [enter link description here](https://stackoverflow.com/questions/53894174/how-to-print-out-a-value-of-object/53894379#53894379) – George Dec 22 '18 at 09:19

2 Answers2

1

You have declared a public $file property into your UploadController class.
So why not just use $this keyword? You can access your $file property wherever you want just by writing:

$this->file

For example:

$this->file = CUploadedFile::getInstance($model,'file');
Igor Shumichenko
  • 394
  • 3
  • 12
  • I tried to use "$this->file " but when I "var_dump($this->file);exit;" I got a null, – George Dec 21 '18 at 13:09
  • $this->file=CUploadedFile::getInstance($model,'file'); $file=$this->file; /* var_dump($file);exit; */ if($model->validate()){ $uploaded = $file->saveAs($dir.'/'.$file->getName()); } } $this->render('index', array( 'model' => $model, 'uploaded' => $uploaded, 'dir' => $dir, )); } public function actionDownload(){ var_dump($this->file);exit; $path = Yii::getPathOfAlias('/yiiroot/trackstar/protected/uploads/')."23602414.pdf"; $upload=new Upload(); $upload->downloadFile($path); } } – George Dec 21 '18 at 13:09
  • Could you tell me why I got a null? In public function actionDownload(){ var_dump($this->file);exit;...} Thank you. – George Dec 21 '18 at 13:24
  • Because your `$file` property is empty. Firstable, you need to write someting to it using your `function actionIndex()` – Igor Shumichenko Dec 21 '18 at 13:58
  • i think it's better to declare `actionIndex()` as `public` method, because you do an ugly things. Firstable you should call `public function actionIndex()` to fill your $file property and the you can use it via `$this->file` in your `actionDownload()` method. GLHF! ;3 – Igor Shumichenko Dec 21 '18 at 14:06
  • Not need to pass variables between functions like Zain Farooq advices (guy who also answered this question) – Igor Shumichenko Dec 21 '18 at 15:47
  • Thank you, I changed my approach to pass a parameter from front to the "actionDownload($id)", I can got an object by "var_dump($this->file);exit;" in my front as below.E:\projects\yiiroot\trackstar\protected\views\upload\index.php:22: object(CUploadedFile)[17] private '_name' => string '23602414.pdf' (length=12) private '_tempName' => string 'D:\wamp\tmp\php33CA.tmp' (length=23) private '_type' => string 'application/pdf' (length=15) private '_size' => int 181004 private '_error' => int 0 private '_e' (CComponent) => null private '_m' (CComponent) => null – George Dec 22 '18 at 07:48
  • I want to know, how to print out the "private '_name' => string '23602414.pdf'"? I tried to use "$this->file->name", but I got a non object error. – George Dec 22 '18 at 07:50
0

You don't need to declare $file as a global variable. Just declare it in your class and then you can use it in any function of a class with $this keyword as $this refers to the class you are in. Like in actionIndex function.

 $this->file=CUploadedFile::getInstance($model,'file');

And in actionDownload()

public function actionDownload(){

    var_dump($this->file);exit; //here printed out a null

    $path = Yii::getPathOfAlias('/yiiroot/trackstar/protected/uploads/')."23602414.pdf";

    $upload=new Upload();

    $upload->downloadFile($path);
}

What does the variable $this mean in PHP?

Here is another reference question

Zain Farooq
  • 2,956
  • 3
  • 20
  • 42
  • Thank you, I rewrited my code as you said, but in var_dump($this->file);exit; I got a error:" Undefined variable: file". Could you tell me why? – George Dec 21 '18 at 12:59
  • Ok, then you can return value in `actionIndex` function and then use that value by passing as a parameter inside `actionDownload` function – Zain Farooq Dec 21 '18 at 13:03
  • Thank you, but when my frontend call the "r=upload/download" I must passing a parameter to "public function actionDownload()" too? Because the frontend will call the "actionDownload()" directly, I don't know how to pass a parameter from actionIndex() to actionDownload(). Could you tell me more detaily? – George Dec 21 '18 at 13:20
  • You must have to call both of the functions. First, assign `actionDownload()` to a variable and then you can pass it to `actionDownload()` – Zain Farooq Dec 21 '18 at 14:48
  • Thats an easy way for you – Zain Farooq Dec 21 '18 at 14:49