Solved

Hyperlink in PHP

Posted on 2014-10-27
3
146 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 108

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now