Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Hyperlink in PHP

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Title # Comments Views Activity
PHP query / monitor data from Telnet to MySQL 7 47
PHP curl issue VERBOSE output 18 86
mysql query for sum() 3 28
PHP 5.6 and 7.x 4 20
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

839 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