Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

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
0
PeterdeB
Asked:
PeterdeB
  • 3
  • 2
  • 2
2 Solutions
 
Ray PaseurCommented:
OK, I'll try to help.  What is the question?
0
 
JayDiabloCommented:
What isn't working exactly?

One issue that I see is that you're defining "$title_clean" and "$file" below the HTML fragment that is assigned to "$string".  Doing it this way is going to give you 200 HTML files (one for each PDF) that are all identical ($string and $file are going to be empty strings, so where you're expecting to see something in those two places, you'll have nothing).

To correct this, put the HTML fragment bit inside your foreach loop below where you define $title_clean.  Like so:

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


PHP substitutes variables when it encounters them, which in this case is when $string is assigned (at the top of your file).  If you want that fragment outside of the loop, there are ways to deal with that (include file, function call, templates).
0
 
Ray PaseurCommented:
Somewhere around line 50 of this example, you would put your file_put_contents() statement to write the HTML files.
http://us.php.net/manual/en/function.file-put-contents.php

HTH, ~Ray
<?php // RAY_temp_peterdeb.php
error_reporting(E_ALL);
echo "<pre>\n";

// CHOOSE THIS DIRECTORY OR ANOTHER DIRECTORY
$dir = getcwd();

// GET ARRAY OF ALL THE FILES IN THIS DIRECTORY
// MAN PAGE: http://us.php.net/manual/en/function.scandir.php
$files = scandir($dir);

// GET THE PDF FILES
$pdfs  = array();
foreach ($files as $file)
{
    // SKIP OUR DIRECTORIES
    if (is_dir($file)) continue;

    // SAVE PDF FILES
    if ( 'pdf' == strtolower(end(explode('.', $file))) ) $pdfs[] = $file;
}

// ACTIVATE THIS TO SEE THE FILES
var_dump($pdfs);

// ITERATE OVER THE LIST OF FILES
foreach ($pdfs as $pdf)
{
    // MAKE THE TITLE
    $pdf_title = str_replace('_', ' ', $pdf);
    $pdf_title = str_replace('.pdf', NULL, $pdf_title);

    // MAKE THE PAGE NAME
    $pdf_page  = str_replace('.pdf', '.html', $pdf);

    // MAKE THE HTML STRING
    $html = <<<HTML
<html>
<head>
<title>$pdf_title</title>
</head>
<body>
<a href="$pdf">SEE $pdf</a>
</body>
</html>
HTML;

    // SHOW THE WORK PRODUCTS
    echo PHP_EOL . "PDF: $pdf";
    echo PHP_EOL . "PDF TITLE $pdf_title";
    echo PHP_EOL . "PDF PAGE $pdf_page";
    echo PHP_EOL . "HTML: " . htmlentities($html);
    echo PHP_EOL;
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PeterdeBAuthor Commented:
@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?
0
 
PeterdeBAuthor Commented:
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

0
 
JayDiabloCommented:
Fine by me, glad we could help. :)
0
 
PeterdeBAuthor Commented:
Thanks very much
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now