【问题标题】:PHP : Change environment where the script is calledPHP:更改调用脚本的环境
【发布时间】:2013-12-04 07:48:38
【问题描述】:

这是我的问题,我有一个 php 脚本,它调用一个启动 ckeditor 的 javascript 脚本,这是一个带有预定义 html 内容的富文本编辑器:

<?PHP
$ref = $_GET['req'];

//~ Launches ckeditor
$h = "<html><head><script src='ckeditor/ckeditor.js'></script>\n";
$h =$h."<link href='sample.css' rel='stylesheet'>\n";
$h =$h."<style>\n";
$h =$h.".cke_textarea_inline \n";
$h =$h."{\n";
$h =$h."padding: 10px;\n";
$h =$h."height: 500px;\n";
$h =$h."overflow: auto;\n";
$h =$h."border: 1px solid gray;\n";
$h =$h."-webkit-appearance: textfield;\n";
$h =$h."}\n";
$h =$h."</style></head><body>\n";
$h = $h."<div style='position:absolute; left:50px; top:120px; width:1200px; height:600px;'>\n" ;
$h =$h."<form action='posteddata.php' method='post'>\n";
$h =$h."<textarea name='article-body' style='height:600px'>\n";

//~ Add a submit button which is irrelevant to my problem
$f = "</textarea><p><input type='submit' value='Submit'></p></form>\n";
$f = $f."<script>CKEDITOR.inline( 'article-body' );</script>\n";
$f = $f."</body></html>\n";

chdir($ref);

if(file_exists("System Specification.html"))
{
    $file = fopen("System Specification.html", "r");
    print $h;
    while (!feof($file))
    {
        print fgets($file,4096);
    }
    print $f;
}
else 
{
    echo "Cannot open file";
}

?>

问题是在我的 html 文件中,我调用了一些具有相对路径的图像,例如

<img src='images/2/1.gif' />

图片文件夹位于我的 $ref 文件夹中。图片不收费,因为脚本没有在好目录中启动。我想强制脚本知道他改变了他的路径,这样他就可以在正确的位置获取图像。我徒劳地尝试了chdir。如果我将图像文件夹与我的 php 脚本放在同一级别,它可以工作,但我不能这样做(这个脚本的目的是在一个大型数据库中,其中不能更改树状结构并且副本会太重了)。 有人有解决这个问题的方法吗?我很难解释这一点,所以如果仍然不是很清楚,您可以提出一些问题以获取更多信息。

【问题讨论】:

  • 我想你错过了$h =$h.中的.,应该是$h.= $h.
  • 好吧,我不习惯 php,但是我的代码可以正常工作,并且可以满足我的期望,问题不在于语法。
  • 是字符串拼接

标签: javascript php html image directory


【解决方案1】:
    //~ Launches ckeditor
        $h = "<html><head><script src='ckeditor/ckeditor.js'></script>\n";
        $h.= "<link href='sample.css' rel='stylesheet'>\n";
        $h.= "<style>\n";
        $h.= ".cke_textarea_inline \n";
        $h.= "{\n";
        $h.= "padding: 10px;\n";
        $h.= "height: 500px;\n";
        $h.= "overflow: auto;\n";
        $h.= "border: 1px solid gray;\n";
        $h.= "-webkit-appearance: textfield;\n";
        $h.= "}\n";
        $h.= "</style></head><body>\n";
        $h.= "<div style='position:absolute; left:50px; top:120px; width:1200px; height:600px;'>\n" ;
        $h.= "<form action='posteddata.php' method='post'>\n";
        $h.= "<textarea name='article-body' style='height:600px'>\n";

//~ Add a submit button which is irrelevant to my problem
$f = "</textarea><p><input type='submit' value='Submit'></p></form>\n";
$f = $f."<script>CKEDITOR.inline( 'article-body' );</script>\n";
$f = $f."</body></html>\n";

【讨论】:

  • 我不知道您要指出什么,但在我看来,我的语法完全一样。无论如何,我的观点不在这里,我的脚本可以正常工作并执行我想要它做的事情,我的问题只是关于读取 html 文件的环境路径。我希望在 $ref 文件夹中考虑它,以便它可以访问 $ref/images 中的图像。
  • 所以你可以试试这个 /images/2/1.gif' />
猜你喜欢
  • 2012-09-11
  • 1970-01-01
  • 2018-07-05
  • 1970-01-01
  • 2019-12-12
  • 1970-01-01
  • 2014-08-06
  • 1970-01-01
  • 2017-12-16
相关资源
最近更新 更多