Code to convert txt files to htm in PHP

Source: Internet
Author: User
  1. /*

  2. Bulk convert all. txt files in a directory to the corresponding HTM file, which contains CSS styles that are easy to read
  3. The generated HTM files are placed in the same directory under the HTM directory
  4. Parameter 1: The path to the directory to be converted
  5. Execute PHP txt2htm.php "c:\\txt\\"
  6. PHP txt2htm.php "/tmp/txt/"
  7. PHP txt2htm.php.
  8. */

  9. $basedir = $argv [1];

  10. if (! $basedir | |! Is_dir ($basedir))
  11. {
  12. Die ("Please input dir.\n");
  13. }
  14. Change the working directory

  15. ChDir ($basedir);

  16. $d = Dir (".");

  17. Create an Output directory

  18. $outputdir = "./htm/";

  19. if (!is_dir ($outputdir)) {
  20. mkdir ($outputdir, 0700);
  21. }
  22. Determine if the creation was successful

  23. if (!is_dir ($outputdir))

  24. {
  25. Die ("Cannot mkdir.\n");
  26. }
  27. while (false!== ($entry = $d->read ()))
  28. {
  29. Judging is not a file

  30. if (Is_file ($entry))

  31. {
  32. $filename =strtolower ($entry);
  33. Determine if the TXT file is not

  34. if (Stristr ($filename, ". txt"))

  35. {
  36. $wfile = $outputdir. basename ($filename, ". txt"). ". HTM ";
  37. If the file already exists, skip

  38. if (file_exists ($wfile))

  39. {
  40. echo "**********". $wfile. "is exists, skip this file**************\n";
  41. Continue
  42. }
  43. if ($str =file_get_contents ($entry))
  44. {
  45. Write styles, and line breaks

  46. $str = "". Str_replace ("\ n", "\ n
    ", $STR);

  47. if ($fp =fopen ($wfile, "w"))
  48. {
  49. if (fwrite ($fp, $str) = = = = FALSE) {
  50. Write failed

  51. echo $wfile. "Cover fail! Fwrite fail\n ";

  52. }else{
  53. echo $wfile. "Cover success!\n";
  54. }
  55. Fclose ($FP);
  56. }else{
  57. Failed to create file

  58. echo $wfile. "Cover fail! fopen fail\n ";

  59. }
  60. }else{
  61. Read failed

  62. echo $wfile. "Cover fail! File_get_contents fail\n ";

  63. }
  64. }
  65. }
  66. }
  67. $d->close ();
  68. ?>

Copy Code

Run: Effect:

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.