2

I have a folder on my server that will receive monthly drops of newsletter files. These drops will occur automatically and I've been asked to write something in PHP to display the list of files as downloadable links while changing the display named based on the name of the file.

The folder I'm looking to is "/var/newsletters" and I'm including code on the index.php page at the root directory. The code I have so far is this:

<?php
$dir = "var/newsletters/";

// Open a directory, and read its contents
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false){
      echo "filename:" . $file . "<br>";
    }
    closedir($dh);
  }
}
?>  

This does display the list of files, but it's only the first step of the process. They are not linked and they are not renamed. These are monthly newsletter files and are named nmmyyyy.pdf (For example, September would be n092017.pdf). What I need to do is convert n092017.pdf to "September 2017" and then create the link, so something like n092017.pdf and n102017.pdf in the directory becomes

<ul>
<li><a href="var/newsletters/n092017.pdf">September 2017</a></li>
<li><a href="var/newsletters/n102017.pdf">October 2017</a></li>
</ul>

I've looked at a few links here:How to list files and folder in a dir (PHP) and List all files in one directory PHP, but found that the code I showed above worked best. What I need help with is displaying the list as links and converting the names. Thank you!

EDIT: I was able to get teh link to work with this code:

<?php
$dir = "var/newsletters/";

// Open a directory, and read its contents
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false){
          echo "   <a href=var/newsletters/$file>Click here</a><br>";   
    closedir($dh);
  }
}
?>  

I'm now working on changing the file name and that is not displaying properly. I'm currently working with it as:

<?php
$dir = "var/newsletters/";

// Open a directory, and read its contents
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false){
      echo "<a href=var/newsletters/$file>
            function getDateFromFileName($filename){
            $dateObj   = DateTime::createFromFormat('m-
Y',substr($filename,1,2).'-'.substr($filename,3,6));
            return $dateObj->format('F Y');
            }
     </a><br>";
    }
    closedir($dh);
  }
}

?>  

EDIT 2

            <?php
                $dir = "var/newsletters/";

                function getDateFromFileName($filename){
                $dateObj   = DateTime::createFromFormat('m-Y',substr($filename,1,2).'-'.substr($filename,3,6));
                return $dateObj->format('F Y');
                }
                // Open a directory, and read its contents
                if (is_dir($dir)){
                  if ($dh = opendir($dir)){
                    while (($file = readdir($dh)) !== false){
                      //echo      $file . "<br>";  OR   //echo  "<a href=var/newsletters/$file>Click here</a><br>";   WORKING     
                      $formatted_date = getDateFromFileName($file);
                    echo "<a href=var/newsletters/$file>{$formatted_date}</a><br>";
                    }
                    closedir($dh);
                  }
                }
            ?>      
M. Baile
  • 23
  • 1
  • 7
  • 1
    Short of Copy/Paste what else did you try? Changing names on the side, how hard would it be to alter the code above to at least show the clickable links? You have to show some effort while asking for help. – sskoko Oct 14 '17 at 16:30
  • I've tried to use: echo 'Click here' and " Click here "; but I receive parse errors on both. – M. Baile Oct 14 '17 at 18:31
  • I was able to get the links to look at the right file by using this: echo " Click here
    "; with the exception that I have an extra "/" at the end of the file name which causes the link to break.
    – M. Baile Oct 14 '17 at 18:58

1 Answers1

1

You can add this function to help with getting the dates from the filenames

function getDateFromFileName($filename){
   $dateObj   = DateTime::createFromFormat('m-Y',substr($filename,1,2).'-'.substr($filename,3,6));
   return $dateObj->format('F Y');
 }

Calling getDateFromFileName("n102017.pdf") prints October 2017

EDIT:

To change the name in your scenario do

<?php
$dir = "var/newsletters/";

function getDateFromFileName($filename){
       $dateObj   = DateTime::createFromFormat('m-Y',substr($filename,1,2).'-'.substr($filename,3,6));
       return $dateObj->format('F Y');
     }
// Open a directory, and read its contents
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false){
            if($file == "." || $file ==".."){
                 continue;
             }
            $formatted_date = getDateFromFileName($file);
          echo "   <a href=var/newsletters/$file>{$formatted_date}</a><br>";   
    closedir($dh);
  }
}
?>  
Norris Oduro
  • 1,019
  • 8
  • 17
  • Looks like just what i'm looking for. I'll work on this later tonight or tomorrow. I'll let you know what I come up with! – M. Baile Oct 14 '17 at 17:03
  • i think you should consider marking the answer if you found it helpful – Norris Oduro Oct 14 '17 at 17:04
  • I was able to get the link to work, but still not able to get the name to transfer. I've edited my original question. – M. Baile Oct 14 '17 at 19:16
  • You can use the trim php function to remove the trailing / – Norris Oduro Oct 14 '17 at 19:19
  • I figured out the trailing / so the link works, but I'm still working on the function you posted to change the name of the file. – M. Baile Oct 14 '17 at 19:26
  • Nooo.. Don't put the function in the echo – Norris Oduro Oct 14 '17 at 19:28
  • I am not with my computer at the moment. I'll update my answer in q bit so you know how to do it – Norris Oduro Oct 14 '17 at 19:32
  • Thank you. Unfortunately, I'm receiving the following: Fatal error: Call to a member function format() on a non-object in /home/content/25/12117325/html/trinity/index_test.php – M. Baile Oct 14 '17 at 20:46
  • Is that the line containing `$dateObj->format('F Y')`?? – Norris Oduro Oct 14 '17 at 20:50
  • It certainly is. Listed as : return $dateObj->format('F Y'); – M. Baile Oct 14 '17 at 20:53
  • Just confirming that I should have the {} characters in the URL link, correct? – M. Baile Oct 14 '17 at 20:55
  • Thank you. I did update my original post with the code under EDIT 2, just in case I missed something. Also, when I had the earlier code that showed the file name int the link, I did notice that it listed four files while I only have two files in that folder. The first was listed as one period "." and the second as two periods ".." and then the n092017.pdf followed by n102017.pdf . I'm not sure what caused that, but wanted to bring it up in case that now means something to you. – M. Baile Oct 14 '17 at 21:02
  • Also, if I comment out the $formatted_date = getDateFromFileName($file); line, and use $file as the name in the URL, the error is gone and I'm back to working links (although with the . and ..). Does that fact that I'm using $file and $filename cause any conflict? – M. Baile Oct 14 '17 at 21:05
  • Not sure about that – Norris Oduro Oct 14 '17 at 21:06
  • But you can use a simple `if else` statement to get rid of the `.`and `..` – Norris Oduro Oct 14 '17 at 21:07
  • I can't thank you enough for the help. I copied your code and placed it into my site. I now get a different error stating Parse error: syntax error, unexpected end of file in /home/content/25/12117325/html/trinity/index_test.php on line 472 . Line 472 is the

    line and the final line of the index.php page.

    – M. Baile Oct 14 '17 at 21:14
  • I guess there is a missing semicolon – Norris Oduro Oct 14 '17 at 21:16
  • 1
    Thanks again for all of your help. I copied and pasted your updated code, but still get an error referencing the last line in the page which is just – M. Baile Oct 15 '17 at 18:48
  • Glad I was able to help :) – Norris Oduro Oct 15 '17 at 19:00