Use php to implement the code to convert txt files to htm
Release: 2016-07-25 08:51:30
Original
1073 people have browsed it
-
-
/*
- Convert all .txt files in a certain directory into corresponding htm files in batches. The htm files contain easy-to-read css styles
- generated htm files Place it under the htm directory in the same directory
- Parameter 1: The path of the directory to be converted
- Execute php txt2htm.php "C:\txt\"
- php txt2htm.php "/tmp/txt/"
- php txt2htm.php .
- */
$basedir=$argv[1];
- if(!$basedir||!is_dir($basedir))
- {
- die("please input dir.n");
- }
- //Change working directory
chdir($basedir);
$d = dir(".");
- //Create output directory
$outputdir="./htm/";
- if(!is_dir($outputdir)){
- mkdir($outputdir, 0700);
- }
- //Determine whether the creation is successful< ;/p>
if(!is_dir($outputdir))
- {
- die("cannot mkdir.n");
- }
- while (false !== ($entry = $d->read ()))
- {
- //Judge whether it is a file
if(is_file($entry))
- {
- $filename=strtolower($entry);
- //Judge whether it is a txt File
if(stristr($filename,".txt"))
- {
- $wfile=$outputdir.basename($filename,".txt").".htm";
- //If the file already exists, skip
if(file_exists($wfile))
- {
- echo "**********".$wfile." is exists ,skip this file****************n";
- continue;
- }
- if($str=file_get_contents($entry))
- {
- //Writing style, and newline
-
$str=" ".str_replace("n", "n ",$str);
- if($fp=fopen($wfile,"w"))
- {
- if (fwrite($fp,$str) === FALSE) {
- //write Entry failed
echo $wfile." cover fail! fwrite failn";
- }else{
- echo $wfile." cover success!n";
- }
- fclose($fp);
- }else{
- //Failed to create file
echo $wfile." cover fail! fopen failn";
- }
- }else{
- //Failed to read
- < ;p> echo $wfile." cover fail! file_get_contents failn";
- }
- }
- }
- }
- $d->close();
- ?>
-
Copy code
Run:
Effect:
|
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31