PeterdeB
asked on
Filename panic lol regarding files and extensions
Hi all
Maybe i have been behind the puter for too long, but the script will just now work.
I have a directory with 200 pdf files, and want to put them on my site. I want one page for every pdf file, see the code for the contents of each file.
The <title></title> tag should contain the name of the pdf file [ without extension ] but some of the names contain _ underscores, and they should be replaced by spaces like:
<title>Some pdf file</title>
Each newly created html page, should be named after the name of the pdf file like:
Some_pdf_file.pdf => Some_pdf_file.html
These should be automatically created by the following code which almost works:
Have rewritten it thousands of times and cannot get it to work fully
Maybe i have been behind the puter for too long, but the script will just now work.
I have a directory with 200 pdf files, and want to put them on my site. I want one page for every pdf file, see the code for the contents of each file.
This is how the output/created html page should look like
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>TITLE OF PDF FILE </title>
<style type=text/css>
body{
width: 800px;
font-family:verdana;
}
.content {
width: 800px;
background-color:#ccc;
border: thin solid #CCC;
}
</script>
</head>
<body>
<center>
<iframe width=90% height=600 src=PDFFILE></iframe>
</center>
</body>
</html>
The <title></title> tag should contain the name of the pdf file [ without extension ] but some of the names contain _ underscores, and they should be replaced by spaces like:
<title>Some pdf file</title>
Each newly created html page, should be named after the name of the pdf file like:
Some_pdf_file.pdf => Some_pdf_file.html
These should be automatically created by the following code which almost works:
<?php
// html fragment
$string = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>'.$title_clean.'</title>
<style type=text/css>
body{
width: 800px;
font-family:verdana;
}
.content {
width: 800px;
background-color:#ccc;
border: thin solid #CCC;
}
</script>
</head>
<body>
<center>
<iframe width=90% height=600 src=' .$file. '></iframe>
</center>
</body>
</html>';
// process every pdf file
$nb = 1;
$files = glob("*.pdf");
foreach ($files as $file) { if (!is_dir($file)){
$nb++;
// extension for newly created webpage
$ext = '.html';
// new name
$file_name = $file.$ext;
// capitalize first word
//$title = ucfirst($file_name);
//replace underscores with spaces
$pattern = '/_/';
$replacement = ' ';
$title_clean = preg_replace($pattern, $replacement, $file);
//open or create
if (!$fhandle = fopen('c:\xampp\htdocs\'.$file.$ext', 'w+')) {
echo "Cannot create file ($file_name)";
exit;
}
// write contents
if (fwrite($fhandle, $string) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
Have rewritten it thousands of times and cannot get it to work fully
OK, I'll try to help. What is the question?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@jaydiablo thanks for the help, with the additions of two extra '}' at the end, your solution works great
@raypasseur same to you, only your solution worked right away, and gave some more insight
Therefor I shall split the points to each 250, does that sound reasonable?
@raypasseur same to you, only your solution worked right away, and gave some more insight
Therefor I shall split the points to each 250, does that sound reasonable?
ASKER
The working version courtesy of jaydiablo...
<?php
// process every pdf file
$nb = 1;
$files = glob("*.pdf");
foreach ($files as $file) { if (!is_dir($file)){
$nb++;
// extension for newly created webpage
$ext = '.html';
// new name
$file_name = $file.$ext;
// capitalize first word
$title = ucfirst($file_name);
//replace underscores with spaces
$pattern = '/_/';
$replacement = ' ';
$title_clean = preg_replace($pattern, $replacement, $file);
// html fragment
$string = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>'.$title_clean.'</title>
<style type=text/css>
body{
width: 800px;
font-family:verdana;
}
.content {
width: 800px;
background-color:#ccc;
border: thin solid #CCC;
}
</script>
</head>
<body>
<center>
<iframe width=90% height=600 src=' .$file. '></iframe>
</center>
</body>
</html>';
//open or create
if (!$fhandle = fopen('c:\xampp\htdocs\'.$file.$ext', 'w+')) {
echo "Cannot create file ($file_name)";
exit;
}
// write contents
if (fwrite($fhandle, $string) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
}
}
Fine by me, glad we could help. :)
ASKER
Thanks very much