PHP:列出与文件名相关的链接

标签 php

我有一个存储库,看起来像这样

directory

在我的 debs/Index.PHP 中,我有一个 PHP 脚本来列出 debs 文件夹中的 .deb 文件,这是我的脚本:

<?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>"; 
?>

脚本的输出是:

output

所以,我想做的是添加一个新列,其中将包含指向 deb 的描述页面(如果存在)的链接。该页面将在 Depiction/"package-name"/index.php

也许我们可以使用 deb 名称“com.name.app1”的开头来获取它的描述文件。

新的输出看起来像这样:

output2

如果有人能帮助我实现这一目标,我将不胜感激!

最佳答案

您只需将链接的 url 指定为相对于当前路径的一个文件夹 (../),然后取您已有文件名的前 x 个字符在你的 $new_array 中,从 com.name.app1 开始计算:

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>"; 
}

已编辑:如果您的文件的第一部分并不总是有一定数量的字符,但它总是被下划线 (_) 分隔(您必须指定一个特定的模式,系统无法为您猜测),您可以像这样更改那部分代码:

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

关于PHP:列出与文件名相关的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23180336/

相关文章:

php - CData section not finished 问题

php - 奇怪的 PHP 错误 : function not recognizing its own parameter

Go 中的 php json_encode

php - 无法诊断我的 MySQL root 用户问题

php - 如何通过javascript函数调用codeigniter函数

javascript - 使用相同文件的 Html/JS/PHP 两个页面看起来不同(仅在 chrome 中)

php - 我应该为类似的用法创建不同的 SQL 表,还是只创建一个带有参数的表?

php - 在Codeigniter中获取sql查询结果并显示在网页上

php - NetBeans IDE - PHP 提示问题

PHP 上传进度状态