原谅这里看似蹩脚的地方。我正在使用只能访问 cpanel 的共享主机,主机不愿意代表我安装 Composer 或 PHPWord。是否可以通过从 github 下载 ZIP 来安装 PHPWord?谢谢
是的,我一直这样做。对于PHPWord ,我会从 GitHub 下载 zip 文件,并将src文件夹的内容移动到名为“lib\PhpOffice\PhpWord”的目录。然后您将需要一个 PHP 类加载器。我总是使用它来自动加载,只要类具有正确的命名空间,情况似乎就是如此。
$GLOBALS['class_path'] = array(__DIR__ . '/lib', __DIR__);
// Set-up class_path superglobal variable using php include_path as basis
if (!array_key_exists('class_path', $GLOBALS)) {
$GLOBALS['class_path'] = array();
foreach (explode(PATH_SEPARATOR, get_include_path()) as $path) {
// substitute __DIR__ path for '.' instead
if ($path == '.') {
array_push( $GLOBALS['class_path'], realpath(__DIR__) );
continue;
}
array_push( $GLOBALS['class_path'], realpath($path) );
}
}
if (!function_exists('import')):
function import($package = '') {
if (empty($package)) {
trigger_error("Package path must be specified.", E_USER_ERROR);
}
$package_bits = explode('\\', $package);
$package_path = implode(DIRECTORY_SEPARATOR, $package_bits) . '.php';
foreach ($GLOBALS['class_path'] as $path) {
$file = $path . DIRECTORY_SEPARATOR . $package_path;
if (file_exists($file)) {
require_once($file);
$entity_name = implode('\\', $package_bits);
if (!(class_exists($entity_name, false) ||
interface_exists($entity_name, false)
|| trait_exists($entity_name, false))) {
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0];
trigger_error("Entity '" . $package . "' not found in file '" . $package_path . "' for import called in " .
$caller['file'] . " on line " . $caller['line'], E_USER_ERROR);
}
return;
}
}
}
endif;
spl_autoload_register('import');
Run Code Online (Sandbox Code Playgroud)
将 $GLOBALS['class_path'] 设置为“ lib ”目录的位置,并确保在尝试实例化PHPWord之前根据需要使用“ use ” 。
希望这可以帮助!
您可以在本地使用Composer,然后将文件上传到服务器。
我在这里询问了这个问题:Using Composer local then uploading files through FTP
基本上,如果您无法在服务器上运行 Composer,请在本地使用它,然后将必要的文件复制到服务器。
您应该使用它的原因是因为它会为您下载依赖项,而无需您手动配置大量内容。这就是 Composer 的重点:)
composer require phpoffice/phpword
Using version ^0.13.0 for phpoffice/phpword
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing pclzip/pclzip (2.8.2)
Downloading: 100%
- Installing phpoffice/common (v0.2.6)
Downloading: 100%
- Installing zendframework/zend-stdlib (2.4.13)
Downloading: 100%
- Installing zendframework/zend-validator (2.4.13)
Downloading: 100%
- Installing zendframework/zend-escaper (2.4.13)
Downloading: 100%
- Installing phpoffice/phpword (v0.13.0)
Downloading: 100%
Run Code Online (Sandbox Code Playgroud)