Create list of specific files in subdirectories

45 Views Asked by At

I have a structure:

  • [catalogues]
  • [catalogue-name-1]
    • [data_html]
      • name-1.html - table of posted values from another form
      • name-2.html - table of posted values from another form
      • name-3.html - table of posted values from another form
      • ...
      • name-n.html - table of posted values from another form
    • catalogue-name-1.html
    • [catalogue-name-2]
      • catalogue-name-2.html
    • [catalogue-name-3]
      • catalogue-name-3.html

index.php

  1. How shall I write the function in catalogue-name-?.html to have merged the content of all data_html-tables in one html-table in catalogue-name-?.html?

  2. How shall I write the index.php to have listed all catalogue-name-?.html files as a list of links?

    • <a href="catalogue-name-1.html">name-1</a>
    • <a href="catalogue-name-2.html">name-2</a>
    • <a href="catalogue-name-3.html">name-3</a>
    • ...
    • <a href="catalogue-name-n.html">name-n</a>
2

There are 2 best solutions below

0
Martin Sereday On

Thank you for the hints. There are 3 PHP files involved: index.php - there is an entry form posting values into the work/data.php file, then values manipulated in it are saved in new HTML files in respective data_html folders to be then shown by work/preview.php. In data.php, there is about this code:

<?php
// directory names 
$dir_catalogues = '../catalogues/';
$dir_catalogue = $dir_catalogues.$_POST['catalogue_name'];
$dir_html = $dir_catalogue.'/data_html/'; 
$dir_csv = $dir_catalogue.'/data_csv/';

//Check if the directory with the name already exists
//Create our directory if it does not exist
if (!is_dir($dir_catalogues)) {
mkdir($dir_catalogues);
echo "Directory ".$dir_catalogues." created<br>";
}
if (!is_dir($dir_catalogue)) {
mkdir($dir_catalogue);
echo "Directory ".$dir_catalogue." created<br>";
}
if (!is_dir($dir_html)) {
mkdir($dir_html);
echo "Directory ".$dir_html." created<br>";
}
if (!is_dir($dir_csv)) {
mkdir($dir_csv);
echo "Directory ".$dir_csv." created";
}

// file names 
$img_name_html = $_POST['img_name'].'.html';
$img_name_csv = $_POST['img_name'].'.csv';
$catalogue_name = 'catalogue-'.$_POST['catalogue_name'].'.html';

// HTML file content 
$html = '
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Images rights information</title>
    <style>
        ...
    </style>
</head>
<body>
<div class="wrapper">
<table cellpadding="5">
<th colspan="2">Image info</th>
<tr class="thumb_row""><td class="logo" colspan="2"><img class="img_thumb" src="'.$_POST['img_use_url'].'" alt="Thumbnail"><h3>'.$_POST['img_title'].'</h3></td></tr>
<tr><td width="140" class="lab">File name</td><td colspan="5">'.$_POST['img_name'].'</td></tr>
<tr><td width="140" class="lab">Description</td><td colspan="5">'.$_POST['img_description'].'</td></tr>
<tr><td width="140" class="lab">Source URL</td><td colspan="5"><a href="'.$_POST['img_source_url'].'" target="_blank">'.$_POST['img_source_url'].'</a></td></tr>
<tr><td width="140" class="lab">Image use URL</td><td colspan="5"><a href="'.$_POST['img_use_url'].'" target="_blank">'.$_POST['img_use_url'].'</a></td></tr>
<tr><td width="140" class="lab">Copyright owner</td><td colspan="5">'.$_POST['img_copyright_owner'].'</td></tr>
<tr><td width="140" class="lab">Author</td><td colspan="5">'.$_POST['img_author'].'</td></tr>
<tr><td width="140" class="lab">Author\'s URL</td><td colspan="5"><a href="'.$_POST['img_author_url'].'" target="_blank">'.$_POST['img_author_url'].'</a></td></tr>
<tr><td width="140" class="lab">License</td><td colspan="5">'.$_POST['img_license'].'</td></tr>
<tr><td width="140" class="lab">License Nr.</td><td colspan="5">'.$_POST['img_license_nr'].'</td></tr>
</table>
</div>
</body>
</html>';
......
// create catalogue HTML file
    file_put_contents($dir_catalogue .'/'.$catalogue_name, $html, FILE_APPEND | LOCK_EX);
0
Martin Sereday On

I made have achieved some progress (found and applied som working scripts). But I stuck at the following: sofar I have in my code about this:

<!-- FILE HEADDINGS -->
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Images rights information</title>
 <style>
  *{font-family: arial, tahoma, verdana;}
  .wrapper{width: 900px; margin:auto;box-sizing:border-box;}
  table{width:100%}
  th{font-size:2.2em; font-weight:bold;}
  .thumb_row{background:#eee;}
  td.logo, th{border-bottom:2px solid #999;}
  .img_thumb{max_height: 180px; width:auto; margin: .2em; float:left;}
  td.logo img{ float: left; margin-right:2em;}
  td a img{border: 2px solid blue;}
  td a:hover img{border: 2px solid orange;}
  td.lab{font-weight:bold;vertical-align:top;}
  td h3{font-size:2em; font-weight:bold}
 </style>
</head>
<body>
<div class="wrapper">
<!-- FILE HEADDINGS END -->
<table cellpadding="5">
<th colspan="2">Image info</th>
<tr class="thumb_row"><td class="logo" colspan="2"><img class="img_thumb" src="'.$_POST['img_use_url'].'" alt="Thumbnail"><h3>'.$_POST['img_title'].'</h3></td></tr>
<tr><td width="140" class="lab">File name</td><td colspan="5">'.$_POST['img_name'].'</td></tr>
<tr><td width="140" class="lab">Description</td><td colspan="5">'.$_POST['img_description'].'</td></tr>
<tr><td width="140" class="lab">Source URL</td><td colspan="5"><a href="'.$_POST['img_source_url'].'" target="_blank">'.$_POST['img_source_url'].'</a></td></tr>
<tr><td width="140" class="lab">Image use URL</td><td colspan="5"><a href="'.$_POST['img_use_url'].'" target="_blank">'.$_POST['img_use_url'].'</a></td></tr>
<tr><td width="140" class="lab">Copyright owner</td><td colspan="5">'.$_POST['img_copyright_owner'].'</td></tr>
<tr><td width="140" class="lab">Author</td><td colspan="5">'.$_POST['img_author'].'</td></tr>
<tr><td width="140" class="lab">Author\'s URL</td><td colspan="5"><a href="'.$_POST['img_author_url'].'" target="_blank">'.$_POST['img_author_url'].'</a></td></tr>
<tr><td width="140" class="lab">License</td><td colspan="5">'.$_POST['img_license'].'</td></tr>
<tr><td width="140" class="lab">License Nr.</td><td colspan="5">'.$_POST['img_license_nr'].'</td></tr>
</table>
<!-- FILE FOOTER -->
</div>
</body>
</html>

// CSV file content

$csv = $_POST['catalogue_name'].'|'.$_POST['img_title'].'|'.$_POST['img_name'].'|'.$_POST['img_description'].'|'.$_POST['img_source_url'].'|'.$_POST['img_copyright_owner'].'|'.$_POST['img_author'].'|'.$_POST['img_author_url'].'|'.$_POST['img_license'].'|'.$_POST['img_license_nr'];

// create catalogue HTML file
 file_put_contents($dir_catalogue .'/'.$catalogue_name.'.html', $html, FILE_APPEND | LOCK_EX);
 file_put_contents($dir_catalogue .'/index.html', $html, FILE_APPEND | LOCK_EX);
// create catalogue CSV file
 file_put_contents($dir_catalogue .'/'.$catalogue_name.'.csv', $csv, FILE_APPEND | LOCK_EX);
 file_put_contents($dir_catalogue .'/index.csv', $csv, FILE_APPEND | LOCK_EX);
}

But the result is that the entire $html code is inserted into the catalog file (I want to have the "headings" added only in the beginning, the all individually saved tables, and the "footer" also always only on end of the HTML-catalog file. The CSV output has all variables put into one endless line. I want to have each record in one line.