Connecting Tech Pros Worldwide Forums | Help | Site Map

Create PHP pages from list of files in directory

kennyjjohnson@gmail.com
Guest
 
Posts: n/a
#1: Mar 11 '06
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_Commercial.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_commercial.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
Guest
 
Posts: n/a
#2: Mar 11 '06

re: Create PHP pages from list of files in directory


kennyjjohnson@gmail.com wrote:[color=blue]
> I am trying to find a script that will make php pages based on the
> file names.
> chevy_car_commercial.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_replace('_', ' ', $fname_noext));
$fname = $outdir . '/' . strtolower($fname_noext) . '.php';
$html = "<html>\n\t<head>\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 "Successfully created $fname\n";
}
}

?>

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


JW


Janwillem Borleffs
Guest
 
Posts: n/a
#3: Mar 11 '06

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


kennyjjohnson@gmail.com
Guest
 
Posts: n/a
#4: Mar 11 '06

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@gmail.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_Commercial.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_commercial.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]

kennyjjohnson@gmail.com
Guest
 
Posts: n/a
#5: Mar 11 '06

re: Create PHP pages from list of files in directory


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

Janwillem Borleffs
Guest
 
Posts: n/a
#6: Mar 11 '06

re: Create PHP pages from list of files in directory


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

Replacing:

$html = "<html>\n\t<head>\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


Closed Thread