Jump to content

hyperlink broken


darenwilko

Recommended Posts

hi guys,

 

have this script all good and working (it shows folders and files within a directory).

although if there are any files in folders that have a 'space' in the file name, its link does not work.

 

eg test.php works

 

test test.php doesn't work

 

any ideas?

 

here is the script

 

<?php
// show directory content
function showDir($dir, $i, $maxDepth){
    $i++;
    if($checkDir = opendir($dir)){
        $cDir = 0;
        $cFile = 0;
        // check all files in $dir, add to array listDir or listFile
        while($file = readdir($checkDir)){
            if($file != "." && $file != ".."){
                if(is_dir($dir . "/" . $file)){
                    $listDir[$cDir] = $file;
                    $cDir++;
                }
                else{
                    $listFile[$cFile] = $file;
                    $cFile++;
                }
            }
        }
        
        // show directories
        if(count($listDir) > 0){
            sort($listDir);
            for($j = 0; $j < count($listDir); $j++){
                echo "
                <tr>";
                    $spacer = "";
                    for($l = 0; $l < $i; $l++) $spacer .= " ";
                    // create link
                    $link = "<a href=\"" . $_SERVER["PHP_SELF"] . "?dir=" . $dir . "/" . 

$listDir[$j] . "\">$listDir[$j]</a>";
                    echo "<td>" . $spacer . $link . "</td>
                </tr>";
                // list all subdirectories up to maxDepth
                if($i < $maxDepth) showDir($dir . "/" . $listDir[$j], $i, $maxDepth);
            }
        }
        
        // show files
        if(count($listFile) > 0){
            sort($listFile);
            for($k = 0; $k < count($listFile); $k++){
                $spacer = "";
                for($l = 0; $l < $i; $l++) $spacer .= " ";



                echo "
                <tr>
                    <td><a href= ". $listFile[$k] ." > " . $spacer . $listFile[$k] . "</a></td>
                </tr>";    

            }


        }        
        closedir($checkDir);
    }
}

if($_GET["dir"] == "" || !is_dir($_GET["dir"])) $dir = getcwd();
else $dir = $_GET["dir"];
// replace backslashes, not necessary, but better to look at
$dir = str_replace("\\", "/", $dir);

// show parent path
$pDir = pathinfo($dir);
$parentDir = $pDir["dirname"];

echo "<a href=\"" . $_SERVER["PHP_SELF"] . "\"><h3>Home</h3></a>";
echo "Current directory: " . $dir;
echo "<a href=\"" . $_SERVER["PHP_SELF"] . "?dir=$parentDir\"><h4>Parent directory: 

$parentDir</h4></a>";

// Display directory content
echo"<table border=1 cellspacing=0 cellpadding=2>
<tr><th align=left>File / Dir</th>";

// specifies the maxDepth of included subdirectories
// set maxDepth to 0 if u want to display the current directory
$maxDepth = 0;
showDir($dir, -1, $maxDepth);  
?> 

 

guessing its something to do with

 

                <tr>
                    <td><a href= ". $listFile[$k] ." > " . $spacer . $listFile[$k] . "</a></td>
                </tr>";

 

but im not sure.

thanks for help!

Link to comment
https://forums.phpfreaks.com/topic/97026-hyperlink-broken/
Share on other sites

haha im so silly

 

                <tr>
                    <td><a href= ". $listFile[$k] ." > " . $spacer . $listFile[$k] . "</a></td>
                </tr>";

 

should be

 

                <tr>
                    <td><a href=' ". $listFile[$k] ." '> " . $spacer . $listFile[$k] . "</a></td>
                </tr>";

 

forgot the ' '

 

;D

Link to comment
https://forums.phpfreaks.com/topic/97026-hyperlink-broken/#findComment-496551
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.