Solved

Filename panic lol regarding files and extensions

Posted on 2011-02-15
7
257 Views
Last Modified: 2012-05-11
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
Comment
Question by:PeterdeB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34898922
OK, I'll try to help.  What is the question?
0
 
LVL 4

Assisted Solution

by:JayDiablo
JayDiablo earned 250 total points
ID: 34898974
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
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 34899096
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:PeterdeB
ID: 34907577
@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
 

Author Comment

by:PeterdeB
ID: 34907608
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
 
LVL 4

Expert Comment

by:JayDiablo
ID: 34907694
Fine by me, glad we could help. :)
0
 

Author Closing Comment

by:PeterdeB
ID: 34924704
Thanks very much
0

Featured Post

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question