Create PHP pages from list of files in directory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kennyjjohnson@gmail.com

    #1

    Create PHP pages from list of files in directory

    Hello, this is probably easy for a php pro... but I cant figure it
    out..

    I have a directory of mp3 files named like this.
    Chevy_Car_Comme rcial.mp3

    There are about 150 files.

    I am trying to find a script that will make php pages based on the file
    names.
    chevy_car_comme rcial.php

    And also dump that variable title
    Chevy Car Commercial into the <title> of the web page...

    I have made dynamic pages before... but that was using a form..

    Anyone know if this is possible?

    Thanks
    kj

  • Janwillem Borleffs

    #2
    Re: Create PHP pages from list of files in directory

    kennyjjohnson@g mail.com wrote:[color=blue]
    > I am trying to find a script that will make php pages based on the
    > file names.
    > chevy_car_comme rcial.php
    >
    > And also dump that variable title
    > Chevy Car Commercial into the <title> of the web page...
    >[/color]

    Using PHP 4.3 or better? Then you could try the following script:

    <?php

    $ext = 'mp3';
    $path = '/path/to/*.mpeg';
    $outdir = '.';

    $glob = glob($path);
    foreach ($glob as $file) {
    $file = basename($file) ;
    $fname_noext = preg_replace("/\.$ext$/", '', $file);
    $title = ucwords(str_rep lace('_', ' ', $fname_noext));
    $fname = $outdir . '/' . strtolower($fna me_noext) . '.php';
    $html = "<html>\n\t<hea d>\n\t\t<title> $title</title>\n\t";
    $html .= "</head>\n\t<body> </body>\n</html>";
    if (!$fp = @fopen($fname, 'w')) {
    print "Unable to create $fname. ";
    print "Please check the permissions of '$outdir'\n";
    continue;
    } else {
    fputs($fp, $html);
    fclose($fp);
    print "Successful ly created $fname\n";
    }
    }

    ?>

    When using PHP v < 4.3, replace the call to the glob function with
    opendir/readdir (see the manual).


    JW


    Comment

    • Janwillem Borleffs

      #3
      Re: Create PHP pages from list of files in directory

      Janwillem Borleffs wrote:[color=blue]
      > $path = '/path/to/*.mpeg';
      >[/color]

      This should be:

      $path = '/path/to/*.mp3';


      JW


      Comment

      • kennyjjohnson@gmail.com

        #4
        Re: Create PHP pages from list of files in directory

        Thank you so much! That works! It made the pages perfectly.
        I thought I would just be able to include my template page in there but
        that does not work.

        I was putting this where you have the $html variables

        $html = "include ("template.php" )";

        I even tried it with the php tags but no dice...

        anyone know how to get my page into that $html variable.

        thanks again.



        kennyjjohnson@g mail.com wrote:[color=blue]
        > Hello, this is probably easy for a php pro... but I cant figure it
        > out..
        >
        > I have a directory of mp3 files named like this.
        > Chevy_Car_Comme rcial.mp3
        >
        > There are about 150 files.
        >
        > I am trying to find a script that will make php pages based on the file
        > names.
        > chevy_car_comme rcial.php
        >
        > And also dump that variable title
        > Chevy Car Commercial into the <title> of the web page...
        >
        > I have made dynamic pages before... but that was using a form..
        >
        > Anyone know if this is possible?
        >
        > Thanks
        > kj[/color]

        Comment

        • kennyjjohnson@gmail.com

          #5
          Re: Create PHP pages from list of files in directory

          Ok, I think I am getting close... let me see... thanks again

          Comment

          • Janwillem Borleffs

            #6
            Re: Create PHP pages from list of files in directory

            kennyjjohnson@g mail.com wrote:[color=blue]
            > Ok, I think I am getting close... let me see... thanks again[/color]

            Replacing:

            $html = "<html>\n\t<hea d>\n\t\t<title> $title</title>\n\t";
            $html .= "</head>\n\t<body> </body>\n</html>";

            With:

            ob_start();
            include 'template.php';
            $html = ob_get_contents ();
            ob_end_clean();

            Should get you close. Mind that all variables you want to use within
            template.php should be created and assigned before the template file is
            included.


            JW


            Comment

            Working...