Solved

Filename panic lol regarding files and extensions

Posted on 2011-02-15
7
235 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 108

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 108

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now