2016-11-01 59 views
0

我有一些与调度程序一起运行的命令,其中一些命令无法按预期工作,如果放在外部文件中,代码工作正常。但是当作为带有调度程序的命令运行时,它不会执行任何操作,也不会删除文件&文件没有输出。Laravel 5.1 - 命令未按预期运行

命令:

<?php 

namespace App\Console\Commands; 

use Illuminate\Console\Command; 
use DirectoryIterator; 

class CleanExports extends Command { 

    protected $name = 'clean:exports'; 

    public function fire() { 
$folderName = 'exports'; 

if (file_exists($folderName)) { 
    $i = 0; 
    foreach (new DirectoryIterator($folderName) as $fileInfo) { 
     if ($fileInfo->isDot()) { 
     continue; 
     } 
     if (time() - $fileInfo->getCTime() >= 1*24*60*60) { 
      unlink($fileInfo->getRealPath()); 
     } 
     $i++; 
    } 

    echo $i.' files cleaned'; 
}   
} 
} 

Kernel.php

<?php 

namespace App\Console;  

use Illuminate\Console\Scheduling\Schedule; 
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;  

class Kernel extends ConsoleKernel { 

    /** 
    * The Artisan commands provided by your application. 
    * 
    * @var array 
    */ 
    protected $commands = [ 
     \App\Console\Commands\Inspire::class, 
     '\App\Console\Commands\CleanExports', 
    ]; 

    /** 
    * Define the application's command schedule. 
    * 
    * @param \Illuminate\Console\Scheduling\Schedule $schedule 
    * @return void 
    */ 
    protected function schedule(Schedule $schedule) { 
     $schedule->command('inspire') 
       ->hourly();  
      $schedule->command('clean:exports')->everyMinute()->sendOutputTo('temp/errors.txt'); 
} 
+0

究竟什么不行?它不删除文件吗?更加详细一些。 – Andrew

+0

你在工作中使用了什么样的“调度程序”? –

+0

和'kernel.php'' \ App \ Console \ Commands \ CleanExports :: class'中的这一行代替这个''\ App \ Console \ Commands \ CleanExports'' –

回答

0

你不会越来越从这个任何输出的原因是因为它file_exists('exports')将会返回false。

尝试增加的绝对路径,而不是:

$folderName = public_path('exports'); 

而且,只是为了好措施,并检查这是正确的添加else输出的东西,如果该目录不存在。

希望这会有所帮助!