John Does Legacy
John Does Legacy

Reputation: 1519

Laravel undefined function

I have following function:

# Get Directories of Path as Array
function dirToArray($dir) {

   $result = array();

   $cdir = scandir($dir);
   foreach ($cdir as $key => $value)
   {
      if (!in_array($value,array(".","..")))
      {
         if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
         {
            $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
         }
         else
         {
            $result[] = $value;
         }
      }
   }

   return $result;
}

and call it like this:

        $watchFolder = 'C:\\xampp\\htdocs\\project\\One\\';
        $this->dirToArray($watchFolder);

But I get following error:

Call to undefined function App\Http\Controllers\dirToArray()

Does anyone know how to solve this?

EDIT: So my full Controller looks like this:

<?php

namespace App\Http\Controllers;

use DB;
use Mail;
use File;
use Input;
use Request;
use Illuminate\Filesystem\Filesystem;
use App\Http\Controllers\Controller;

class ProductRController extends Controller
{

    public function createProduct()
    {
            ...
            $watchFolder = 'C:\\xampp\\htdocs\\Product\\R\\';
            $this->dirToArray($watchFolder);

            return redirect()->route('root')->with('message', 'Success')->withInput();
        }
        else
        {
            return redirect()->route('newProduct')->with('message', 'Error')->withInput();
        }

    }

    # Get Directories of Path as Array
    function dirToArray($dir) {

       $result = array();

       $cdir = scandir($dir);
       foreach ($cdir as $key => $value)
       {
          if (!in_array($value,array(".","..")))
          {
             if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
             {
                $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
             }
             else
             {
                $result[] = $value;
             }
          }
       }

       return $result;
    }

}

Upvotes: 1

Views: 1867

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163798

You still need to use $this-> inside a function as you did it outside the function, because $this represents an object on which you're calling your method:

$result[$value] = $this->dirToArray($dir . DIRECTORY_SEPARATOR . $value);

Upvotes: 1

Related Questions