(PHP 5, PHP 7, PHP 8)

DOMDocument::saveHTMLFile Copia el documento interno a un fichero usando el formato HTML


public DOMDocument::saveHTMLFile(string $filename): int|false

Crea un documento HTML desde la representación DOM. Esta función normalmente se llama después de construir un nuevo documento desde cero, como en el ejemplo de abajo.



La ruta al documento HTML guardado.

Valores devueltos

Devuelve el número de bytes escritos o false si ocurrió un error.


Ejemplo #1 Guardar un árbol HTML en un archivo


= new DOMDocument('1.0');
// queremos una impresión buena
$doc->formatOutput = true;

$root = $doc->createElement('html');
$root = $doc->appendChild($root);

$head = $doc->createElement('head');
$head = $root->appendChild($head);

$title = $doc->createElement('title');
$title = $head->appendChild($title);

$text = $doc->createTextNode('Este es el título');
$text = $title->appendChild($text);

'EScrito: ' . $doc->saveHTMLFile("/tmp/test.html") . ' bytes'; // Escrito: 129 bytes


Ver también

add a note

User Contributed Notes 3 notes

RiKdnUA at mail dot ru
10 years ago
saveHTMLFile() always saves the file in UTF-8. Even if the DOMDocument->encoding explicitly prescribe different from UTF-8 encoding. All "non-Latin" characters will be converted to HTML-entities. Tested in PHP 5.2.9-2 and PHP 5.2.17. Example:

=new domDocument('1.0', 'WINDOWS-1251');
$document->loadHTML('<html><head><title>Russian language</title></head><body>Русский язык</body></html>');
"Записано байт. Recorded bytes: ".$document->saveHTMLFile('html.html');

Method recorded file in UTF-8 encoding. The contents of the file html.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Russian language</title>
<body>Ðóññêèé ÿçûê</body>
naebeth at hotmail dot NOSPAM dot com
11 years ago
Not mentioned in the documentation is the fact that using DOMDocument::saveHTMLFile() will automatically overwrite the contents if an existing file is used - with no notice, warning or error thrown.

Make sure you check the filename before using this function so that you don't accidentally overwrite important files.


= fopen('test.html', 'w');
fwrite($file, 'this is some text');

$doc = new DOMDocument();
$doc->formatOutput = true;

// test.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">


If you're dynamically generating a series of pages using DOMDocument objects, make sure you are also dynamically generating the file or directory names using something that can't easily be confused for an existing file/folder, or check if the desired path already exists before saving so that you don't accidentally delete previous files.
deep42thouSPAMght42 at y_a_h_o_o dot com
13 years ago
I foolishly assumed that this function was equivalent to
($filename, $document->saveHTML());
but there are differences in the generated HTML:
= new DOMDocument();
$doc->encoding = 'iso-8859-1';

#<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "">

#<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "">
#<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Test</title></head><body></body></html>

Note that saveHTMLFile() adds a UTF-8 meta tag despite the ISO-8859-1 document encoding.
To Top