简体   繁体   中英

PHP - directory listing script - display file names WITHOUT file extensions

Help to modify this script to hide the file extension in the listing. Clicking on links in the listing must function as before - ie open

function display_block( $file )
{
    global $ignore_file_list, $ignore_ext_list, $force_download;

    $file_ext = getFileExt($file);
    if( !$file_ext AND is_dir($file)) $file_ext = "dir";
    if(in_array($file, $ignore_file_list)) return;
    if(in_array($file_ext, $ignore_ext_list)) return;

    $download_att = ($force_download AND $file_ext != "dir" ) ? " download='" . basename($file) . "'" : "";

    $rtn = "<div class=\"block\">";
    $rtn .= "<a href=\"$file\" class=\"$file_ext\"{$download_att}>";
    //$rtn .= " <div class=\"img $file_ext\">&nbsp;</div>";
    $rtn .= "   <div class=\"name\">\n";
    $rtn .= "       <div class=\"file\">" . basename($file) . "</div>\n";
    //$rtn .= "     <div class=\"date\">Size: " . format_size($file) . "<br />Last modified: " .  date("D. F jS, Y - h:ia", filemtime($file)) . "</div>\n";
    $rtn .= "   </div>\n";
    $rtn .= "   </a>\n";
    $rtn .= "</div>";
    return $rtn;
}

You can use pathinfo function to get filename without extension as below:

function display_block($file) {
    global $ignore_file_list, $ignore_ext_list, $force_download;
    $file_ext = getFileExt($file);
    if (!$file_ext AND is_dir($file))
        $file_ext = "dir";
    if (in_array($file, $ignore_file_list))
        return;
    if (in_array($file_ext, $ignore_ext_list))
        return;

    $filePathInfo = pathinfo($file); 
    $download_att = ($force_download AND $file_ext != "dir" ) ? " download='" . basename($file) . "'" : "";

    $rtn = "<div class=\"block\">";
    $rtn .= "<a href=\"$file\" class=\"$file_ext\"{$download_att}>";
//$rtn .= " <div class=\"img $file_ext\">&nbsp;</div>";
    $rtn .= "   <div class=\"name\">\n";
    $rtn .= "       <div class=\"file\">" . $filePathInfo['filename'] . "</div>\n";
//$rtn .= "     <div class=\"date\">Size: " . format_size($file) . "<br />Last modified: " .  date("D. F jS, Y - h:ia", filemtime($file)) . "</div>\n";
    $rtn .= "   </div>\n";
    $rtn .= "   </a>\n";
    $rtn .= "</div>";
    return $rtn;
}

basename function is used when we are sure about the file extension. if you are sure that file extension will be same then you can also use it as below:

$fileName = basename($file, ".php"); 

Hope it helps you!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM