Solved

Filename panic lol regarding files and extensions

Posted on 2011-02-15
7
245 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 109

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 109

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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 

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

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.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 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