为什么在使用命令行执行文件时不会评估我的PHP定义的常量?

时间:2013-07-26 13:21:50

标签: php wordpress command-line dreamhost

我正在为WP站点创建一个插件,我必须从另一个目录初始导入大量文件。 (该插件运行良好,等等)我想使用命令行运行导入。如果在本地(Windows)上执行,一切都可以,但是当我从Linux bash执行时,定义的常量不会被计算,而是打印为字符串。

define("ASCOR_RECORDINGS_TBL", $wpdb->prefix . "recordings");

我是否需要进行任何设置才能评估PHP定义的常量?我的网站在Dreamhost服务器上运行。

当我执行以下行时:

[myserver]$ /usr/local/php5/bin/php /home/path/to/import.php

我明白了:

WordPress database error Table 'db_ro.ASCOR_RECORDINGS_TBL' doesn't exist for query INSERT INTO `ASCOR_RECORDINGS_TBL` ...........

我执行的文件的内容:

<?php
require_once dirname(dirname(dirname(dirname(__FILE__)))) . "/wp-load.php";

require_once "class/AscorDbHelper.php";


$origin = realpath("/path/to/files");

$upload_dir = wp_upload_dir();
$subdir = $upload_dir['subdir'];


mkdir(ASCOR_UPLOAD_DIR . "/" . $subdir, 777, true);

require_once $origin . "/classes/PConfFiles.inc.php";

$db = new AscorDbHelper();

$cf = new PConfFiles('/^PC_([0-9\-]+)_(.*)\.([a-z0-9]{2,4})$/i', $origin);
$list = $cf->getFilesList();

$catPC = $db->addCategory("Special");
$catOther = $db->addCategory("Other");

if($list){
    $pc = $db->addAuthor("Ciprian V.");

    foreach($list as $rec){
        $fileUrl = $subdir . "/" . $rec[0];
        $desc = str_replace("-", " ", $rec[2]);

        copy(realpath($origin . "/" . $rec[0]), ASCOR_UPLOAD_DIR . "/" . $fileUrl );

        $db->addRecording($fileUrl, $catPC->id, $pc->id, $desc, null, $rec[1]);

        echo "Added: " . $rec[0] . "\n";
    }
}


$cf = new PConfFiles('/^([0-9\-]+)\_([^\_]+)_(.*)\.([a-z0-9]{2,4})$/i', $origin);
$list = $cf->getFilesList();

if($list){
    foreach($list as $rec){
        $authorName = str_replace("-", " ", $rec[2]);
        $date = $rec[1];
        $desc = str_replace("-", " ", $rec[3]);
        $fileUrl = $subdir . "/" . $rec[0];

        $authorId = $db->getAuthorIdOrSaveIt($authorName);

        copy(realpath($origin . "/" . $rec[0]), ASCOR_UPLOAD_DIR . "/" . $fileUrl );

        $db->addRecording($fileUrl, $catOther->id, $authorId, $desc, null, $date);

        echo "Added: " . $rec[0] . "\n";
    }
}

echo "done";

常量在插件的主文件中定义:

define("ASCOR_RECORDINGS_TBL", $wpdb->prefix . "recordings");
define("ASCOR_RECORDINGS_AUTHORS_TBL", $wpdb->prefix . "recordings_authors");
define("ASCOR_RECORDINGS_CATEGORIES_TBL", $wpdb->prefix . "recordings_categories");

define("ASCOR_RECORDS_PER_PAGE", 50);

define("ASCOR_EXTEND_DIR", dirname(__FILE__));
define("ASCOR_EXTEND_URL", plugins_url("", __FILE__));

define("ASCOR_NOTIFY_UPDATED", "updated");
define("ASCOR_NOTIFY_ERROR", "error");

define("ASCOR_UPLOAD_DIR", ABSPATH . "/wp-content/uploads/recordings");

1 个答案:

答案 0 :(得分:1)

是否加载了文件?在其中引入一个解析错误,看它是否失败。并在Windows上试用它。