Fouad
Fouad

Reputation: 409

PHP: List Links related to a file name

I have a Repository, that looks like this

directory

In my debs/Index.PHP i have a PHP scripts to list the .deb files inside debs folder, that's my script:

<?PHP
#  directory
$directory = dir("./");

# Extension Filter, comment to disable:
 $allowed_ext = array(".deb", ".txt", ".ext", ".ext", ".ext", ".ext"); 


$do_link = TRUE; 
$sort_what = 0; //0:y name; 1: by size; 2:by date
$sort_how = 0; //0: ASCENDING;    1:DESCENDING


# # #
function dir_list($dir){ 
    $i=0; 
    $dl = array(); 
    if ($hd = opendir($dir))    { 
        while ($sz = readdir($hd)) {  
            if (preg_match("/^\./",$sz)==0) $dl[] = $sz;$i.=1;  
        } 
    closedir($hd); 
    } 
    asort($dl); 
    return $dl; 
} 
if ($sort_how == 0) { 
    function compare0($x, $y) {  
        if ( $x[0] == $y[0] ) return 0;  
        else if ( $x[0] < $y[0] ) return -1;  
        else return 1;  
    }  
    function compare1($x, $y) {  
        if ( $x[1] == $y[1] ) return 0;  
        else if ( $x[1] < $y[1] ) return -1;  
        else return 1;  
    }  
    function compare2($x, $y) {  
        if ( $x[2] == $y[2] ) return 0;  
        else if ( $x[2] < $y[2] ) return -1;  
        else return 1;  
    }  
}else{ 
    function compare0($x, $y) {  
        if ( $x[0] == $y[0] ) return 0;  
        else if ( $x[0] < $y[0] ) return 1;  
        else return -1;  
    }  
    function compare1($x, $y) {  
        if ( $x[1] == $y[1] ) return 0;  
        else if ( $x[1] < $y[1] ) return 1;  
        else return -1;  
    }  
    function compare2($x, $y) {  
        if ( $x[2] == $y[2] ) return 0;  
        else if ( $x[2] < $y[2] ) return 1;  
        else return -1;  
    }  

} 

################################################## 
#    We get the information here 
################################################## 

$i = 0; 
while($file=$directory->read()) { 
    $file = strtolower($file);
    $ext = strrchr($file, '.');
    if (isset($allowed_ext) && (!in_array($ext,$allowed_ext)))
        {
            // dump 
        }
    else { 
        $temp_info = stat($file); 
        $new_array[$i][0] = $file; 
        $new_array[$i][1] = $temp_info[7]; 
        $new_array[$i][2] = $temp_info[9]; 
        $new_array[$i][3] = date("F d, Y", $new_array[$i][2]); 
        $i = $i + 1; 
        } 
} 
$directory->close(); 

################################################## 
# We sort the information here 
################################################# 

switch ($sort_what) { 
    case 0: 
            usort($new_array, "compare0"); 
    break; 
    case 1: 
            usort($new_array, "compare1"); 
    break; 
    case 2: 
            usort($new_array, "compare2"); 
    break; 
} 

############################################################### 
#    We display the infomation here 
############################################################### 

$i2 = count($new_array); 
$i = 0; 
echo "<table class='CSSTableGenerator'> 
                <tr> 
                    <td width=355>File name (Download)</td> 
                    <td align=center width=70>File Size</td> 
                    <td align=center width=100>Last Modified</td> 
                </tr>"; 
for ($i=0;$i<$i2;$i++) { 
    if (!$do_link) { 
        $line = "<tr><td>" .  
                        $new_array[$i][0] .  
                        "</td><td>" .  
                        number_format(($new_array[$i][1]/1024)) .  
                        " KB"; 
        $line = $line  . "</td><td>" . $new_array[$i][3] . "</td></tr>"; 
    }else{ 
        $line = '<tr><td align=left ><A class="ex1" HREF="' .   
                        $new_array[$i][0] . '">' .  
                        $new_array[$i][0] .  
                        "</A></td><td>"; 
        $line = $line . number_format(($new_array[$i][1]/1024)) .  
                        " KB"  . "</td><td>" .  
                        $new_array[$i][3] . "</td></tr>"; 
    } 
    echo $line; 
} 
echo "</table>"; 
?>

The output of the scripts is:

output

So, What i want to do is to add a new column, which will contain a link to the Depiction page of the deb if it exists. The page will be in Depiction/"package-name"/index.php

Maybe we can use the beginning of the deb name "com.name.app1" to get the depiction file for it.

The new output would look like that:

output2

I would appreciate if someone can help me achieve this!

Upvotes: 3

Views: 478

Answers (2)

benomatis
benomatis

Reputation: 5643

You just have to specify the url for the link as one folder up relative to the current path (../), then take the first x number of characters of the file name you already have in your $new_array counted from com.name.app1 as you stated:

if (!$do_link) { 
    $line = "<tr><td>" . $new_array[$i][0];
    $line .= '</td><td><a href="' . '../Depiction/' . substr($new_array[$i][0], 0, strlen("com.name.app1")) . '/index.php">Depiction</a>';
    $line .= "</td><td>" . number_format(($new_array[$i][1]/1024)) . " KB"; 
    $line .= "</td><td>" . $new_array[$i][3] . "</td></tr>"; 
}

EDITED: If the first section of your file doesn't always have a set number of characters, but it is always divided by an underscore (_) for example (you will have to specify a specific pattern, the system cannot guess it for you), you could change that portion of the code like this:

substr($new_array[$i][0], 0, strpos($new_array[$i][0], "_"))

Upvotes: 1

Zibadian
Zibadian

Reputation: 101

If it is not important to have an extra column for the link, then you can set the $do_link to true. If you set $do_link to true then the filename will become clickable.

If you want to have the extra column for the download then change the following line:

        $line = "<tr><td>" .  
                    $new_array[$i][0] .  
                    "</td><td>" .  
                    number_format(($new_array[$i][1]/1024)) .  
                    " KB";

into

    $line = "<tr><td>" . $new_array[$i][0] .  "</td>".
         '<a class="ex1" href="' .$new_array[$i][0] . '">Depiction</a></td>'.
         "<td>" . number_format(($new_array[$i][1]/1024)) .  " KB";

The table only lists existing files, so all files will be downloadable.

Upvotes: 0

Related Questions