-1

我的网站:www.test.com/abc/test

php文件:content.php

<!-- content starts here -->
<h2>Welcome test page</h2>
<p>This is test page.</p>
<p><img src="/abc/test/img.jpg" /></p>
<!-- content ends here -->

我具有执行文件并下载为 doc 文件的功能。我正在使用“include_once()”方法来执行 PHP 文件,也正在使用适当的头文件将该文件下载为 doc 文件。

一切似乎都很好,除了图像。因为我使用“相对路径”来引用图像。Doc 文件在绝对路径下工作正常,但它没有加载相对路径的图像。

问题:我不想将 content.php 文件替换为图像的“绝对路径”。使用任何 PHP 预定义函数,有一种方法可以读取文件并替换“相对于绝对”路径并在不修改源文件的情况下下载文件?

4

2 回答 2

2

您可以使用 html <base>(内部head部分)标签并为您的图像设置“根”。

例如:

<head>
..
<base href="http://yourdomain.com/optional_base_folder_to_all_images/" />
..
</head>
于 2013-07-16T08:50:16.600 回答
0

也许您可以使用 file_get_contents 将源代码放入变量中,然后您可以将所有图像路径替换为 str_replace

例如:

$source_code = file_get_contents(path/to/file.php);

$new_source = str_replace('<img src="/', '<img src="http://www.test.com/abc/test/', $source_code);
于 2013-07-16T08:50:30.377 回答