Hi everyone,

Just signed up this forum, great to see so many active posts with replies!

Anyway, straight into the coding issue and I would be very gratetful for your help....

I am displaying a list of PDF files from a folder using an array.

Currently the output on the web page looks something like this:

<a href='test/A.pdf'>A.pdf</a>
<a href='test/B.pdf'>B.pdf</a>

But the output should be like this (without displaying file extension on the web page) :

<a href='test/A.pdf'>A</a>
<a href='test/B.pdf'>B</a>

I have tried a number of ways to prevent the file extension from displaying on the web page, but no matter what I try the file extension keeps appearing 😕

I would really appreciate some help on this 🙂

Here is my full PHP code:

                    //get folder name from query string
                    $str_subcat = $_GET['subcat'];

                $Folder = "pdfs/$str_subcat/";

                $narray=array();
                $i=0;

                // Open the folder                 
                $DirHandle = @opendir($Folder) or die($Folder." could not be opened.");           


                while($file = readdir($DirHandle))
                {
                    if(is_dir($file))
                    {
                        continue;
                    }
                   //remove . and .. characters displayed by server.
                   //only display PDF files in the directory.
                    else if($file != '.' && $file != '..' && strpos($file, '.pdf'))
                    {
                        //echo "<a href='$path/$file'>$file</a><br/>";

                        $narray[$i]=$file;                            
                        $i++;                                                        
                    }

                }
                sort($narray);



                for($i=0; $i<sizeof($narray); $i++)
                {

                    echo "<tr>";
                    echo "<td width=100% class=bodycopy>";
                    echo "<a href='".$Folder.$narray[$i].$file."' target=blank>".$narray[$i].$file."</a>";
                    echo "</td>";
                }


                // Close the handle to the directory
                closedir($DirHandle); 

    One option might be to use [man]preg_replace[/man] for the name:

    $name = preg_replace('#^(.*)(\.[^\.]*)?$#', '$1', $file);
    

    Then use $name where you want $file without the suffix.

      NogDog;10889460 wrote:

      One option might be to use [man]preg_replace[/man] for the name:

      $name = preg_replace('#^(.*)(\.[^\.]*)?$#', '$1', $file);
      

      Then use $name where you want $file without the suffix.

      Hi NogDog,

      I tried what you mention above, but it still does not seem to remove the file extension?

      Here is my code (new/changes in red) with the preg_replace but the output is still the same as before:

                          //get folder name from query string
                          $str_subcat = $_GET['subcat'];
      
                      $Folder = "pdfs/$str_subcat/";
      
                      $narray=array();
                      $i=0;
      
                      // Open the folder                 
                      $DirHandle = @opendir($Folder) or die($Folder." could not be opened.");           
      
      
                      while($file = readdir($DirHandle))
                      {
                          if(is_dir($file))
                          {
                              continue;
                          }
                         //remove . and .. characters displayed by server.
                         //only display PDF files in the directory.
                          else if($file != '.' && $file != '..' && strpos($file, '.pdf'))
                          {
                              //echo "<a href='$path/$file'>$file</a><br/>";
      
                              $narray[$i]=$file;                            
                              $i++;                                                        
                          }
      
                      }
                      sort($narray);
      
      
                     [COLOR="Red"]$name = preg_replace('#^(.*)(\.[^\.]*)?$#', '$1', $file);[/COLOR]
      
      
                      for($i=0; $i<sizeof($narray); $i++)
                      {
      
                          echo "<tr>";
                          echo "<td width=100% class=bodycopy>";
      [COLOR="Gray"]                        //echo "<a href='".$Folder.$narray[$i].$file."' target=blank>".$narray[$i].$file."</a>";[/COLOR]
      
                          echo "<a href='".$Folder.$narray[$i].$file."' target=blank>".$narray[$i].[COLOR="Red"]$name[/COLOR]."</a>";
                          echo "</td>";
                      }
      
      
                      // Close the handle to the directory
                      closedir($DirHandle);
      

        You would want to use it in the output loop that follows against the $narray[$i] value, not $file. In fact, it looks like there is no reason to use $file within that loop, as it's value is going to be boolean false at that point.

          Thanks NogDog.

          I solved it by using str_replace. Got code from another forum.

          echo "<a href='".$Folder.$narray[$i].$file."' target=blank>".str_replace(".pdf", "", $narray[$i].$file)."</a>";
          

          Hope the above code helps someone else out with similar problem.

            I solved it by using str_replace.

            What happens if the filename is "abcd.pdf.efgh.pdf"? You will end up with "abcd.efgh" instead of "abcd.pdf.efgh".

              laserlight;10889535 wrote:

              What happens if the filename is "abcd.pdf.efgh.pdf"? You will end up with "abcd.efgh" instead of "abcd.pdf.efgh".

              This is why I love regex so much. While preg may be technically slower than say str_replace by example, it offers so much more robust control and fine tuning capabilities. For light weight tasks, the differences in processing speeds should be negligible.

                nrg_alpha wrote:

                This is why I love regex so much. While preg may be technically slower than say str_replace by example, it offers so much more robust control and fine tuning capabilities. For light weight tasks, the differences in processing speeds should be negligible.

                Speaking of that, [man]preg_replace/man can function as a drop-in replacement for str_replace() here:

                echo "<a href='".$Folder.$narray[$i].$file."' target=blank>"
                    . preg_replace('/\.pdf$/i', '', $narray[$i].$file)."</a>";

                The above example accounts for differing case as well. However, I note that you should use [man]htmlspecialchars/man when printing output to the browser, but this was not done.

                  Never thought of using preg_replace as a drop-in replacement. Nice touch 🙂

                    laserlight;10889539 wrote:

                    Speaking of that, [man]preg_replace/man can function as a drop-in replacement for str_replace() here:

                    echo "<a href='".$Folder.$narray[$i].$file."' target=blank>"
                        . preg_replace('/\.pdf$/i', '', $narray[$i].$file)."</a>";

                    The above example accounts for differing case as well. However, I note that you should use [man]htmlspecialchars/man when printing output to the browser, but this was not done.

                    Nrg_alpha and laserlight thanks so much for the tip! I will use the preg_replace() instead.

                      No problem lol (actually, Nog and Laserlight came up with preg solutions.. I just merely stated my preference 😃 But hey, if there's free credit to be taken, I'm in!)

                        Of course, since it's known the file name ends with the four characters ".pdf" and that it's the last four characters that are to be removed, it's hardly necessary to go looking for them. Just remove the last four characters. substr($name,0,-4).

                        Actually, that's not known: the filter only makes sure the filename contains ".pdf" and the only place it can't appear is right at the start. It will, for example, allow "foo.pdf.tgz", and that's how it would be displayed.

                        The following three lines do the work of reading all the filenames listed in $Folder and retaining those that end with ".pdf". Basically, the stuff from "// Open the folder" down to "sort".

                        if(!is_readable($Folder)) die($Folder." could not be opened.");
                        $narray = array_map('basename',glob("$Folder/*.pdf"));
                        sort($narray);
                        

                        I see there's also a check to ensure that "foo.pdf" isn't a directory named "foo.pdf". Annoyingly, there is a flag to ensure glob() only returns directories, but no flag to ensure that glob() only returns nondirectories.

                        $narray = array_map('basename',glob("$Folder/*.pdf"));
                        $narray = array_diff($narray, array_map('basename',glob("$Folder/*.pdf", GLOB_ONLYDIR)));
                        

                        Incidentally, the call to sort() may be unnecessary and perhaps undesirable: glob has a flag to turn off sorting.

                          FWIW, my original suggestion was trying to be more generic, getting rid of any file extension if there is one, and leaving it unchanged if there is no suffix. If you know 100% that every file in question will have a specific suffix (such as by using Weedpacket's glob() suggestion), then you could just use substr() with a 0 offest and -4 length.

                            ianhman;10889457 wrote:

                            Hi everyone,

                            Just signed up this forum, great to see so many active posts with replies!

                            Anyway, straight into the coding issue and I would be very gratetful for your help....

                            I am displaying a list of PDF files from a folder using an array.

                            Currently the output on the web page looks something like this:

                            <a href='test/A.pdf'>A.pdf</a>
                            <a href='test/B.pdf'>B.pdf</a>

                            But the output should be like this (without displaying file extension on the web page) :

                            <a href='test/A.pdf'>A</a>
                            <a href='test/B.pdf'>B</a>

                            I have tried a number of ways to prevent the file extension from displaying on the web page, but no matter what I try the file extension keeps appearing 😕

                            I would really appreciate some help on this 🙂

                            Here is my full PHP code:

                                                //get folder name from query string
                                                $str_subcat = $_GET['subcat'];
                            
                                            $Folder = "pdfs/$str_subcat/";
                            
                                            $narray=array();
                                            $i=0;
                            
                                            // Open the folder                 
                                            $DirHandle = @opendir($Folder) or die($Folder." could not be opened.");           
                            
                            
                                            while($file = readdir($DirHandle))
                                            {
                                                if(is_dir($file))
                                                {
                                                    continue;
                                                }
                                               //remove . and .. characters displayed by server.
                                               //only display PDF files in the directory.
                                                else if($file != '.' && $file != '..' && strpos($file, '.pdf'))
                                                {
                                                    //echo "<a href='$path/$file'>$file</a><br/>";
                            
                                                    $narray[$i]=$file;                            
                                                    $i++;                                                        
                                                }
                            
                                            }
                                            sort($narray);
                            
                            
                            
                                            for($i=0; $i<sizeof($narray); $i++)
                                            {
                                                echo "<tr>";
                                                echo "<td width=100% class=bodycopy>";
                                                echo "<a href='".$Folder.$narray[$i].$file."' target=blank>".$narray[$i].$file."</a>";
                                                echo "</td>";                    }
                            
                            
                                            // Close the handle to the directory
                                            closedir($DirHandle); 
                            

                            I have a solution, instead of just using the $file variable, you can use the pathinfo($path . $file) function which will return an array of important variables (I am assuming you have PHP 5.2+) Here is your code fixed so it implements the function ;

                                                //get folder name from query string
                                                $str_subcat = $_GET['subcat'];
                            
                                            $Folder = "pdfs/$str_subcat/";
                            
                                            $narray=array();
                                            $i=0;
                            
                                            // Open the folder                 
                                            $DirHandle = @opendir($Folder) or die($Folder." could not be opened.");           
                            
                            
                                            while($file = readdir($DirHandle))
                                            {
                                                if(is_dir($file))
                                                {
                                                    continue;
                                                }
                                                //only display PDF files in the directory.
                                                else if(strpos($file, '.pdf'))
                                                {
                                                    $pinfo = pathinfo($file);
                                                    $file = $pinfo['filename'];
                                                    //echo "<a href='$path/{$file}.pdf'>$file</a><br/>";
                            
                                                    $narray[$i]=$file;                            
                                                    $i++;                                                        
                                                }
                            
                                            }
                                            sort($narray);
                            
                            
                            
                                            for($i=0; $i<sizeof($narray); $i++)
                                            {
                                                echo "<tr>";
                                                echo "<td width=100% class=bodycopy>";
                                                echo "<a href='".$Folder.$narray[$i].".pdf' target=blank>".$narray[$i]."</a>";
                                                echo "</td>";
                                            }
                            
                            
                                            // Close the handle to the directory
                                            closedir($DirHandle); 
                            

                            Please correct me if I'm wrong

                              Write a Reply...