Solved

Hyperlink in PHP

Posted on 2014-10-27
3
150 Views
Last Modified: 2014-11-12
When I run this code it lists the files from the cabinet_layout directory, but my hyperlinks are missing "cabinet_layouts" in the url.

cabinet_layouts is a sub-directory in the root directory.

<?PHP
  //$dir = "/cabinet_layouts/";
  // Change directory
  
  $dir = chdir("cabinet_layouts");
  
  function getFileList($dir)
  {
    // array to hold return value
    $retval = array();

    // add trailing slash if missing
    if(substr($dir, -1) != "/") $dir .= "/";

    // open pointer to directory and read list of files
    $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
    while(false !== ($entry = $d->read())) {
      // skip hidden files
      if($entry[0] == ".") continue;
      if(is_dir("$dir$entry")) {
        $retval[] = array(
          "name" => "$dir$entry/",
          "type" => filetype("$dir$entry"),
          "size" => 0,
          "lastmod" => filemtime("$dir$entry")
        );
      } elseif(is_readable("$dir$entry")) {
        $retval[] = array(
          "name" => "$dir$entry",
          "type" => mime_content_type("$dir$entry"),
          "size" => filesize("$dir$entry"),
          "lastmod" => filemtime("$dir$entry")
        );
      }
    }
    $d->close();

    return $retval;
  }
?>

<h1>List PDF files with links</h1>

<table class="collapse" border="1">
<thead>
<tr><th>Name</th><th>Type</th><th>Size</th><th>Last Modified</th></tr>
</thead>
<tbody>
<?PHP
  $dirlist = getFileList("./");
  foreach($dirlist as $file) {
    if($file['type'] != "application/pdf") continue;
    echo "<tr>\n";
    echo "<td><a href=\"{$file['name']}\">",basename($file['name']),"</a></td>\n";
    echo "<td>{$file['type']}</td>\n";
    echo "<td>{$file['size']}</td>\n";
    echo "<td>",date('r', $file['lastmod']),"</td>\n";
    echo "</tr>\n";
  }
?>
</tbody>
</table>

Open in new window

Ed. note: Code moved into Code snippet.
0
Comment
Question by:mfinocc
3 Comments
 

Author Comment

by:mfinocc
ID: 40407170
Aaah! I see. Thanks.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40407220
You might try something like this.  For obvious reasons I am not going to leave it on my server, but feel free to copy it and install it for your own use.

<?php // demo/temp_mfinocc.php
error_reporting(E_ALL);

// PUT THIS SCRIPT IN THE WEB ROOT OR OTHER TOP-LEVEL DIRECTORY
$path = realpath(getcwd()) . DIRECTORY_SEPARATOR;

// DEFINE THE FILE NAME SUFFIX YOU WANT TO USE.  DEFAULT: PDF
$suf = !empty($_GET['q']) ? trim($_GET['q']) : 'pdf';
$suf = '.' . $suf;

// CREATE THE REGULAR EXPRESSION TO MATCH THE FILE NAME SUFFIX
$rgx
= '#'              // REGEX DELIMITER
. preg_quote($suf) // THE SUFFIX
. '$'              // AT THE END OF STRING
. '#'              // REGEX DELIMITER
. 'i'              // FLAG FOR CASE-INSENSITIVE
;

// SEE http://php.net/manual/en/class.recursivedirectoryiterator.php#85805
$objs = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);

// UNFORTUNATELY THIS DOES NOT WORK AT PHP 5.4
// var_dump($objs);

// ITERATE OVER THE OBJECTS
foreach($objs as $name => $obj)
{
    // SKIP FILES THAT DO NOT MATCH OUR OBJECTIVE
    if (!preg_match($rgx, $name)) continue;

    // CONSTRUCT A LINK
    $name = str_replace($path, NULL, $name);
    $url
    = '<a target="_blank" '
    . 'href="'
    . $name
    . '">'
    . $name
    . '</a>'
    ;
    echo PHP_EOL . '<br>' . $url;
}

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
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.

786 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