Russian language

DOMDocument::saveHTMLFile

Creates an HTML document from the DOM representation. This function is usually called after building a new dom document from scratch as in the example below.

Parameters

The path to the saved HTML document.

Return Values

Returns the number of bytes written or false if an error occurred.

Examples

Example #1 Saving a HTML tree into a file

$doc = new DOMDocument ( ‘1.0’ );
// we want a nice output
$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 ( ‘This is the title’ );
$text = $title -> appendChild ( $text );

echo ‘Wrote: ‘ . $doc -> saveHTMLFile ( «/tmp/test.html» ) . ‘ bytes’ ; // Wrote: 129 bytes

See Also

  • DOMDocument::saveHTML() — Dumps the internal document into a string using HTML formatting
  • DOMDocument::loadHTML() — Load HTML from a string
  • DOMDocument::loadHTMLFile() — Load HTML from a file

User Contributed Notes 3 notes

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:

$document =new domDocument ( ‘1.0’ , ‘WINDOWS-1251’ );
$document -> loadHTML ( ‘Русский язык‘ );
$document -> formatOutput = true ;
$document -> encoding = ‘WINDOWS-1251’ ;
echo «Записано байт. Recorded bytes: » . $document -> saveHTMLFile ( ‘html.html’ );
?>

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

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.

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

$doc = new DOMDocument ();
$doc -> formatOutput = true ;
$doc -> loadHTML ( ‘ ‘ );
$doc -> saveHTMLFile ( ‘test.html’ );

?>

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.

I foolishly assumed that this function was equivalent to
file_put_contents ( $filename , $document -> saveHTML ());
?>
but there are differences in the generated HTML:
$doc = new DOMDocument ();
$doc -> loadHTML (
‘ ‘
);
$doc -> encoding = ‘iso-8859-1’ ;

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

  • DOMDocument
    • _​_​construct
    • createAttribute
    • createAttributeNS
    • createCDATASection
    • createComment
    • createDocumentFragment
    • createElement
    • createElementNS
    • createEntityReference
    • createProcessingInstruction
    • createTextNode
    • getElementById
    • getElementsByTagName
    • getElementsByTagNameNS
    • importNode
    • load
    • loadHTML
    • loadHTMLFile
    • loadXML
    • normalizeDocument
    • registerNodeClass
    • relaxNGValidate
    • relaxNGValidateSource
    • save
    • saveHTML
    • saveHTMLFile
    • saveXML
    • schemaValidate
    • schemaValidateSource
    • validate
    • xinclude

    Источник

    PHP File Create/Write

    In this chapter we will teach you how to create and write to a file on the server.

    PHP Create File — fopen()

    The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files.

    If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).

    The example below creates a new file called «testfile.txt». The file will be created in the same directory where the PHP code resides:

    Example

    PHP File Permissions

    If you are having errors when trying to get this code to run, check that you have granted your PHP file access to write information to the hard drive.

    PHP Write to File — fwrite()

    The fwrite() function is used to write to a file.

    The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written.

    The example below writes a couple of names into a new file called «newfile.txt»:

    Example

    $myfile = fopen(«newfile.txt», «w») or die(«Unable to open file!»);
    $txt = «John Doe\n»;
    fwrite($myfile, $txt);
    $txt = «Jane Doe\n»;
    fwrite($myfile, $txt);
    fclose($myfile);
    ?>

    Notice that we wrote to the file «newfile.txt» twice. Each time we wrote to the file we sent the string $txt that first contained «John Doe» and second contained «Jane Doe». After we finished writing, we closed the file using the fclose() function.

    If we open the «newfile.txt» file it would look like this:

    PHP Overwriting

    Now that «newfile.txt» contains some data we can show what happens when we open an existing file for writing. All the existing data will be ERASED and we start with an empty file.

    In the example below we open our existing file «newfile.txt», and write some new data into it:

    Example

    $myfile = fopen(«newfile.txt», «w») or die(«Unable to open file!»);
    $txt = «Mickey Mouse\n»;
    fwrite($myfile, $txt);
    $txt = «Minnie Mouse\n»;
    fwrite($myfile, $txt);
    fclose($myfile);
    ?>

    If we now open the «newfile.txt» file, both John and Jane have vanished, and only the data we just wrote is present:

    PHP Append Text

    You can append data to a file by using the «a» mode. The «a» mode appends text to the end of the file, while the «w» mode overrides (and erases) the old content of the file.

    In the example below we open our existing file «newfile.txt», and append some text to it:

    Example

    $myfile = fopen(«newfile.txt», «a») or die(«Unable to open file!»);
    $txt = «Donald Duck\n»;
    fwrite($myfile, $txt);
    $txt = «Goofy Goof\n»;
    fwrite($myfile, $txt);
    fclose($myfile);
    ?>

    If we now open the «newfile.txt» file, we will see that Donald Duck and Goofy Goof is appended to the end of the file:

    Complete PHP Filesystem Reference

    For a complete reference of filesystem functions, go to our complete PHP Filesystem Reference.

    Источник

    PHP Create File

    Summary: in this tutorial, you will learn a couple of ways to create a new file in PHP.

    Creating a file using the fopen() function

    The fopen() function opens a file. It also creates a file if the file doesn’t exist. Here’s the syntax of the fopen() function:

    fopen ( string $filename , string $mode , bool $use_include_path = false , resource $context = ? ) : resourceCode language: PHP (php)

    To create a new file using the fopen() function, you specify the $filename and one of the following modes:

    Mode File Pointer
    ‘w+’ At the beginning of the file
    ‘a’ At the end of the file
    ‘a+’ At the end of the file
    ‘x’ At the beginning of the file
    ‘x+’ At the beginning of the file
    ‘c’ At the beginning of the file
    ‘c+’ At the beginning of the file

    Except for the ‘a’ and ‘a+’ , the file pointer is placed at the beginning of the file.

    If you want to create a binary file, you can append the character ‘b’ to the $mode argument. For example, the ‘wb+’ opens a binary file for writing.

    The following example uses fopen() to create a new binary file and write some numbers to it:

     $numbers = [1, 2, 3, 4, 5]; $filename = 'numbers.dat'; $f = fopen($filename, 'wb'); if (!$f) < die('Error creating the file ' . $filename); > foreach ($numbers as $number) < fputs($f, $number); >fclose($f); Code language: HTML, XML (xml)
    • First, define an array of five numbers from 1 to 5.
    • Second, use the fopen() to create the numbers.dat file.
    • Third, use the fputs() function to write each number in the $numbers array to the file.
    • Finally, close the file using the fclose() function.

    Creating a file using the file_put_contents() function

    The file_put_contents() function writes data into a file. Here’s the syntax of the file_put_contents() function:

    file_put_contents ( string $filename , mixed $data , int $flags = 0 , resource $context = ? ) : intCode language: PHP (php)

    If the file specified by the $filename doesn’t exist, the function creates the file.

    The file_put_contents() function is identical to calling the fopen() , fputs() , and fclose() functions successively to write data to a file.

    The following example downloads a webpage using the file_get_contents() function and write HTML to a file:

     $url = 'https://www.php.net'; $html = file_get_contents($url); file_put_contents('home.html', $html);Code language: HTML, XML (xml)
    • First, download a webpage https://www.php.net using the file_get_contents() function.
    • Second, write the HTML to the home.html file using the file_put_contents() function

    Summary

    • Use the fopen() function with one of the mode w , w+ , a , a+ , x , x+ , c , c+ to create a new file.
    • Use the file_put_contents() function to create a file and write data to it.
    • The file_put_contents() function is identical to calling fopen() , fputs() , and fclose() functions successively to write data to a file.

    Источник

    PHP Create Word Document from HTML

    In this article, you will learn how to create a word document in doc or docx using the PHP programming language.

    The conversion of HTML into MS Word Document is primarily utilised in the web application to produce .doc/.docx records with dynamic HTML content. The server-side script is needed to dynamically export data from HTML to Word Document. The MS Word document can be quickly generated with HTML content using a PHP script. There are many third-party libraries available for HTML to Word conversion. But, you can easily convert the HTML content to a Word document using PHP without any external library.

    PHP provides features for adding HTTP headers. Like, we can set the HTTP header to download the content or attachment, and we can also set it to show a ‘Save as‘ dialog box while downloading on the browser. So in this example, we have added the content headers to make the generated doc file downloadable. For file formatting, we have utilized word-accommodating CSS. It is important to use inline CSS instead of an external CSS file.

    Create And Download Word Document in PHP

    Here is the script to generate and download a word document. So, copy and paste this code either on the localhost or on the server, only you will have to change the body element content.

     $filename = 'demo.doc'; header("Content-Type: application/force-download"); header( "Content-Disposition: attachment; filename color: #0000ff;">$filename)); header( "Content-Description: File Transfer"); @readfile($filename); $content = ' xmlns:v="urn:schemas-microsoft-com:vml" ' .'xmlns:o="urn:schemas-microsoft-com:office:office" ' .'xmlns:w="urn:schemas-microsoft-com:office:word" ' .'xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"= ' .'xmlns="http://www.w3.org/TR/REC-html40">' .' http-equiv="Content-Type" content="text/html; charset=Windows-1252">' .' ' .'
    Читайте также:  Css text link width
Оцените статью