Jump to content

Links to files not working in php


flopez1974

Recommended Posts

I am having difficulty figuring out why links to the results of my query are not working. In other words, users search for a folder name ex. 22-21-11, PDF files appear that reside in this folder as links. When a user clicks on the link, nothing happens. Everything is working great except for this portion. I am a PHP newbie and a nudge in the right direction will be greatly appreciated.  I am suspecting the path I am using but I will let the PHP Jedi's comment. 

 

 

<?php // CMM_Tino.php
error_reporting(E_ALL);
 
/**
 * SEARCH FOR PDF FILES BASED ON DIRECTORY NAME
 */
 
// ACTIVATE THIS TO SHOW THE REQUEST
// var_dump($_GET);
 
// LINKS TO THE PDF FILES, IF ANY
$links = NULL;
 
// PUT THE PATH TO THE DIRECTORY OF FILE FOLDERS HERE
$path  = 'D:/CMM_Search/CMM';
 
// GET THE LIST OF FILE FOLDERS
$folders = array_diff(scandir($path), array('.','..'));
 
// FILTER THE LIST OF FILE FOLDERS TO REMOVE EXTRANEOUS FILES
foreach ($folders as $key => $name)
{
    if (!is_dir($path . DIRECTORY_SEPARATOR . $name)) unset($folders[$key]);
}
 
// IF THERE IS A REQUEST
$q = !empty($_GET['q']) ? $_GET['q'] : NULL;
if (!empty($q))
{
    // IF THE REQUEST MATCHES ONE OF THE FILE FOLDERS
    $folder = NULL;
    foreach ($folders as $folder)
    {
        if (strpos($folder, $q) !== FALSE) break;
        $folder = NULL;
    }
    if ($folder)
    {
        $links = "<p><b>$folder</b></p>" . PHP_EOL;
 
        // GET THE LIST OF FILES IN THE FOLDER
        $files = array_diff(scandir($path . DIRECTORY_SEPARATOR . $folder), array('.','..'));
 
        // PREPARE LINKS TO THE PDF FILES
        foreach ($files as $file)
        {
            $end = explode('.', $file);
            $end = end($end);
            $end = strtoupper($end);
            if ($end != 'PDF') continue;
 
            $links .= '<br>'
            . '<a target="my_pdf" '
            . 'href="'
            . $path
            . DIRECTORY_SEPARATOR
            . $folder
            . DIRECTORY_SEPARATOR
            . $file
            . '">'
            . $file
            . '</a>'
            . PHP_EOL
            ;
        }
    }
    else
    {
        $links = '<br>'
        . 'Unknown Folder: '
        . "<b>$q</b>"
        . PHP_EOL
        ;
    }
}
 
// CREATE THE SEARCH PAGE USING PHP HEREDOC NOTATION
$htm = <<<EOD
<!DOCTYPE html>
<html>
<head>
<title>Barfield CMM Lookup</title>
<meta name="ROBOTS" content="NOINDEX, NOFOLLOW" />
<!-- CSS styles for standard search box -->
<style type="text/css">
#tfheader{
background-color:#;
}
#tfnewsearch{
float:left;
padding:20px;
        width: 1686px;
    }
.tftextinput{
margin: 0;
padding: 5px 15px;
font-family: Arial, Helvetica, sans-serif;
font-size:14px;
border:1px solid #0076a3; border-right:0px;
border-top-left-radius: 5px 5px;
border-bottom-left-radius: 5px 5px;
}
.tfbutton {
margin: 0;
padding: 5px 15px;
font-family: Arial, Helvetica, sans-serif;
font-size:14px;
outline: none;
cursor: pointer;
text-align: center;
text-decoration: none;
color: #ffffff;
border: solid 1px #0076a3; border-right:0px;
background: #0095cd;
background: -webkit-gradient(linear, left top, left bottom, from(#00adee), to(#0078a5));
background: -moz-linear-gradient(top,  #00adee,  #0078a5);
border-top-right-radius: 5px 5px;
border-bottom-right-radius: 5px 5px;
}
.tfbutton:hover {
text-decoration: none;
background: #007ead;
background: -webkit-gradient(linear, left top, left bottom, from(#0095cc), to(#00678e));
background: -moz-linear-gradient(top,  #0095cc,  #00678e);
}
/* Fixes submit button height problem in Firefox */
.tfbutton::-moz-focus-inner {
 border: 0;
}
.tfclear{
clear:both;
}
    .auto-style1 {
        width: 915px;
        height: 90px;
        margin-left: 0px;
    }
    .auto-style2 {
        color: #0033CC;
    }
</style>
</head>
<body>
<!-- HTML for SEARCH BAR -->
<div id="tfheader">
<br />
                                                                                                                                       
        <img alt="Barfield" class="auto-style1" src="Barfield.jpg" /><br />
<!-- ************** NOTE THE $links VARIABLE WILL BE FILLED IN BY THE PHP SCRIPT ************** -->
$links
        <br />
<!-- ************** NOTE THE ACTION ATTRIBUTE IS REMOVED TO CAUSE THIS SCRIPT TO SEND THE FORM REQUEST TO THE CURRENT URL ************** -->
<form id="tfnewsearch" method="get">
                                                                                                                                                                                                                 
                <span class="auto-style2"><strong>CMM</strong></span>    
       <input type="text" class="tftextinput" name="q" size="21" maxlength="120"><input type="submit" value="search" class="tfbutton">   
</form>
 
   <br />
<div class="tfclear"></div>
</div>
 
<!-- Use of this code assumes agreement with the Google Custom Search Terms of Service. -->
<!-- The terms of service are available at http://www.google.com/cse/docs/tos.html -->
<form name="cse" id="searchbox_demo" action="http://www.google.com/cse">
  <input type="hidden" name="cref" value="" />
  <input type="hidden" name="ie" value="utf-8" />
  <input type="hidden" name="hl" value="" />
</form>
</body>
</html>
EOD;
 
// WRITE THE HTML DOCUMENT TO THE BROWSER
echo $htm;

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.