Link to home
Start Free TrialLog in
Avatar of PeterdeB
PeterdeBFlag for Netherlands

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.

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>

Open in new window


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;
    }

Open in new window



Have rewritten it thousands of times and cannot get it to work fully
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

OK, I'll try to help.  What is the question?
SOLUTION
Avatar of JayDiablo
JayDiablo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PeterdeB

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?
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;
    }
}
}

Open in new window

Fine by me, glad we could help. :)
Thanks very much