我在根目录中设置了一个cronjob'cron_parent.php'。它的工作原理如下:
foreach()
,为每个相关子域包含cron_child.php 所有子域都包含相同的php文件和功能。当然,我在多个子域上使用它时遇到了麻烦,因为
<b>Fatal error</b>: Cannot redeclare fserror() (previously declared in
/home/***hidden***/public_html/demo/dbconfig.php:28)
in <b>/home/***hidden***/public_html/dev/dbconfig.php</b> on line <b>44</b><br />
我意识到include()
可能不是正确的选择。有没有办法从cron_parent.php运行cron_child.php的'detached'?
编辑:添加了来自cron_parent.php的代码:
<?php
date_default_timezone_set("Europe/Oslo");
header('Content-Type: text/html; charset=utf-8');
$root = dirname(dirname(__FILE__));
$directories = glob($root . '/*' , GLOB_ONLYDIR);
foreach($directories as $path)
{
if(is_file("$path/cron_child.php"))
{
include("$path/cron_child.php");
}
}
出于测试目的,cron_child.php现在只包含此内容:
<?php
include "functions.php";
return false;
答案 0 :(得分:2)
这样的东西应该起作用(只是结构):
// main.php
<?php
require_once('jobHandler.php');
foreach (new DirectoryIterator('your/folder') as $fileInfo) {
// If it is not folder skip;
if($fileInfo->isDot() || ! $file->isDir) continue;
$worker = new jobHandler($folderToProcess);
$worker->run();
}
// jobHandler.php
<?php
class jobHandler
{
public function __construct($folder)
{
// validation here
}
public function run()
{
echo $this->folder . PHP_EOL;
// Do your work here.
}
private $folder = null;
}
您可以将作业配置存储在客户文件夹中。