【发布时间】:2016-03-15 15:08:12
【问题描述】:
我有以下功能:
# 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;
}
然后这样称呼它:
$watchFolder = 'C:\\xampp\\htdocs\\project\\One\\';
$this->dirToArray($watchFolder);
但我收到以下错误:
调用未定义函数 App\Http\Controllers\dirToArray()
有人知道怎么解决吗?
编辑: 所以我的完整控制器看起来像这样:
<?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;
}
}
【问题讨论】:
-
你把你的函数放在哪里了?
-
我把它放在控制器里面
-
你在哪里叫它?在这里工作/不工作级别也无关紧要,但是将方法显式声明为
public是最佳实践(和 PHP 标准) -
你能用相应的文件名更新你的问题吗?
-
尝试使用
$result[$value] = $this->dirToArray($dir . DIRECTORY_SEPARATOR . $value);
标签: php arrays function laravel undefined