找不到“COM”类

时间:2012-05-21 00:18:06

标签: php xampp

我一直在尝试在我的脚本中打开word文档,但是我收到了同样的错误。

 Fatal error: Class 'COM' not found in /Applications/XAMPP/xamppfiles/htdocs/**/**.php on line 3

我的代码:

<?php

$word = new COM("word.application") or die("Unable to instantiate Word"); 

$word->Visible = 1; 


$word->Documents->Open("wordfile.docx");
$temp = $word->Dialogs->Item(228); // returns wdDialogToolsWordCount dialog object
$temp->Execute();   //updates the word count
$numwords = $temp->Words(); //gets the words out of it

echo 'Word count = '.$numwords;

$word->Quit(); 

?>

我尝试更改php.ini并从COM部分删除分号。

[com]
path to a file containing GUIDs, IIDs or filenames of files with TypeLibs
com.typelib_file = 
allow Distributed-COM calls
com.allow_dcom = true
autoregister constants of a components typlib on com_load()
com.autoregister_typelib = true
register constants casesensitive
com.autoregister_casesensitive = false
show warnings on duplicate constat registrations
com.autoregister_verbose = true 

仍然得到同样的错误。

我在mac上使用XAMMP,在Linux上使用基于Web的托管。

4 个答案:

答案 0 :(得分:136)

从PHP 5.4.5开始,COM和DOTNET不再内置到php核心中。你必须在php.ini中添加COM支持:

[COM_DOT_NET]
extension=php_com_dotnet.dll

否则你会在错误日志中看到这个: 致命错误:未找到“COM”类

该扩展包含在PHP 5.4.5 for Windows中。

答案 1 :(得分:7)

请参阅COM Requirements

  

COM功能仅适用于Windows版本的PHP。

     

.Net支持需要PHP 5和.Net运行时。

答案 2 :(得分:0)

我在其中一个项目中使用的是PHP 5.6.14版,ext_目录中包含php_com_dotnet.dll文件,但php.ini文件中未添加相同文件。因此,您需要在php.ini文件中添加“ extension = php_com_dotnet.dll”行。在浏览器中检查phpinfo()以确保已启用它。 希望这会对某人有所帮助。

答案 3 :(得分:-1)

使用Windows Server2019,Apache 32Bit(C:\ Apache24),PHP 32Bit(C:\ PHP)

正如jross写道:php_com_dotnet.dll的完整路径是我的解决方案(扩展名为C:\ PHP \ ext \ php_com_dotnet.dll)。