Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php dom中的save()将数字保存在文件末尾_Php_Dom_Save - Fatal编程技术网

php dom中的save()将数字保存在文件末尾

php dom中的save()将数字保存在文件末尾,php,dom,save,Php,Dom,Save,下面的代码成功地保存了子div,但也在文件末尾保存了一些数字。我认为这是存在的数据字节,我如何摆脱它保存的数字 $file = '../userfolders/'.$email.'/'.$ongrassdb.'/'.$pagenameselected.'.php'; $doc = new DOMDocument(); $doc->load($file); $ele = $doc->createElement('div', $textcon); $ele ->setAttribu

下面的代码成功地保存了子div,但也在文件末尾保存了一些数字。我认为这是存在的数据字节,我如何摆脱它保存的数字

$file = '../userfolders/'.$email.'/'.$ongrassdb.'/'.$pagenameselected.'.php';
$doc = new DOMDocument();
$doc->load($file);
$ele = $doc->createElement('div', $textcon);
$ele ->setAttribute('id', $divname);
$ele ->setAttribute('style', 'background: '.$divbgcolor.'; color :'.$divfontcolor.' ;display : table-cell;');
$element = $doc->getElementsByTagName('div')->item(0);
$element->appendChild($ele);
$doc->appendChild($element);
$myfile = fopen($file, "a+") or die('Unable to open file!');
$html = $doc->save($file);
fwrite($myfile,$html);
fclose($myfile);

我不想使用saveHTML或saveHTMLFile,因为它会创建div的多个实例并向其中添加html标记。

我删除了最后两行,解决了这个问题

fwrite($myfile,$html);
fclose($myfile);
该方法将DOM树保存到文件中,并返回写入文件的字节数。该数字存储在$html中,然后由fwrite附加到同一文件中


只需删除fopen、fwrite和fclose调用。

能否提供包含所有变量的完整文件以及正在加载的文件?
$doc->load($file);
...
$myfile = fopen($file, "a+") or die('Unable to open file!');
$html = $doc->save($file);
fwrite($myfile,$html);
fclose($myfile);