Solved

Hyperlink in PHP

Posted on 2014-10-27
3
154 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 110

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

679 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