note 110149 added to domdocument.savehtmlfile
| From: | naebeth at hotmail dot NOSPAM dot com | Date: | Mon, 24 Sep 2012 10:03:18 +0000 |
| Subject: | note 110149 added to domdocument.savehtmlfile | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190990@lists.php.net to get a copy of this message | ||
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.
Example:
<?php
$file = fopen('test.html', 'w');
fwrite($file, 'this is some text');
fclose($file);
$doc = new DOMDocument();
$doc->formatOutput = true;
$doc->loadHTML('<html><head><title>Test</title></head><body></body></html>');
$doc->saveHTMLFile('test.html');
// test.html
/*
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test</title>
</head>
<body></body>
</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.
----
Server IP: 69.147.83.199
Probable Submitter: 94.216.97.37
----
Manual Page -- http://www.php.net/manual/en/domdocument.savehtmlfile.php
Edit -- https://master.php.net/note/edit/110149
Del: integrated -- https://master.php.net/note/delete/110149/integrated
Del: useless -- https://master.php.net/note/delete/110149/useless
Del: bad code -- https://master.php.net/note/delete/110149/bad+code
Del: spam -- https://master.php.net/note/delete/110149/spam
Del: non-english -- https://master.php.net/note/delete/110149/non-english
Del: in docs -- https://master.php.net/note/delete/110149/in+docs
Del: other reasons-- https://master.php.net/note/delete/110149
Reject -- https://master.php.net/note/reject/110149
Search -- https://master.php.net/manage/user-notes.php