使用COM和Word删除系统tmp文件

时间:2012-11-15 17:47:49

标签: php com ms-word temp

我正在使用COM将动态创建的单词docx转换为PDF。一切都运行得很好,但我们的c:\ windows \ temp \目录变得非常大(我们的C盘上的磁盘空间有限)。下面是我用来生成PDF的代码。有没有办法删除文件关闭后添加到系统临时目录的临时文件?此外,在doc文档转换为pdf之后,我们不再需要它(因此,如果删除tmp文件会破坏单词doc,则无关紧要。)

 /Word Doc to PDF using Com
ini_set("com.allow_dcom","true");

try{
    $word = new com('word.application');
}
catch (com_exception $e)
{
    $nl = "<br />";
    echo $e->getMessage() . $nl;
    echo $e->getCode() . $nl;
    echo $e->getTraceAsString();
    echo $e->getFile() . " LINE: " . $e->getLine();
    $word->Quit();
    $word = null;
    die;

}

$word->Visible = 0; //don't open word gui on server
$word->DisplayAlerts = 0; //don't allow any alerts to open 
//open doc
try{
    $doc = $word->Documents->Open(DOC_LOCATION . "temp_doc/" .  $filename . ".docx");
}
catch (com_exception $e)
{
    $nl = "<br />";
    echo $e->getMessage() . $nl;
    echo $e->getCode() . $nl;
    echo $e->getFile() . " LINE: " . $e->getLine();
    $word->Quit();
    $word = null;
    die;
}
try{
    $doc->ExportAsFixedFormat(DOC_LOCATION . "temp_pdf/" . $filename . ".pdf", 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);

}
catch (com_exception $e)
{
    $nl = "<br />";
    echo $e->getMessage() . $nl;
    echo $e->getCode() . $nl;
    echo $e->getTraceAsString();
    echo $e->getFile() . " LINE: " . $e->getLine();
    $word->Quit();
    $word = null;
    die;
}
$word->Quit();
$word = null;

1 个答案:

答案 0 :(得分:0)

不要说什么版本,但对于XP-您可以使用简单的批处理文件删除 \ Documents and Settings \ USER \ Local Settings \ temp \ 中的文件。例如:

C:
cd \Documents and Settings\USER\Local Settings\Temp
attrib -R -S -H *.* /S /D
DEL /S /F /Q *.*

相应修改。