如何更改 PHP 文件中嵌入 HTML 的样式?

Posted

技术标签:

【中文标题】如何更改 PHP 文件中嵌入 HTML 的样式?【英文标题】:How to change style of embdeded HTML in a PHP file? 【发布时间】:2021-08-02 23:07:00 【问题描述】:

所以我目前正在进行 14 天的实习,我的工作是重新设计网页,但是这些网页是为了显示文件夹中文件的链接而制作的,一旦你点击这些文件,你就可以下载它们。

我的问题是 -> 有一种方法可以使用外部 CSS 文件更改此页面的样式吗?我的问题是我根本不懂 php,也没有时间学习它,所以我不知道这些文件到底是做什么的,但我知道在底部有 html 代码在网站上传递,我尝试更改 HTML 中的 CSS 文件,但它没有改变任何内容,这就是页面的显示方式。

这是图片的链接: https://imgur.com/skjQEk6

这是与 .xls 文件位于同一文件夹中的代码

对不起,代码太长了,我不知道如何缩短它。

    <?php
//
// --- Make your customizations below.  All customizations set here will apply ONLY to this indexed directory --- \\
//
//
// $HomeDir - Absolute path (not url) to TotalIndex script (usually something like: /home/user/public_html/TotalIndex/
// on Linux and something like: c:/inetpub/wwwroot/TotalIndex/ on Windows systems).
// Be sure to include the ending slash in the home directory
//
$HomeDir = "/dcweb/totalindex/";


// Include config file - DO NOT REMOVE
include $HomeDir."config.php";



// To turn off any readme file descriptions (explained in the config.php file) just uncomment the next line (This will prevent a folder description from appearing at the top of the page)...

// $Allow_Readme = 0;


// To exclude a certain file name from the index listing, just add another line of code with the name of the file that
// you want to exclude from the list.

//

// EXAMPLE - If you want to exclude a file named "hidden.txt", then add the following line below:

// $Exclude_File = "hidden.txt";

//

// You can add as many of these as you wish to exclude

// - This is case sensitive -

//
$Exclude_File[] = "exclude_me.txt";



// To exclude a certain folder name from the index listing, just add another line of code with the name of the folder that

// you want to exclude from the list.

//

// EXAMPLE - If you want to exclude a folder named "hidden_folder", then add the following line below:

// $Exclude_File = "hidden_folder";

//

// You can add as many of these as you wish to exclude

// - This is case sensitive -
//
$Exclude_Folder[] = "hidden_folder";



// To exclude a files with certain extensions from the index listing, just add another line of code with the file extension that

// you want to exclude from the list.

//

// EXAMPLE - If you want to exclude file with the extension "txt", then add the following line below:
// $Exclude_Extension = "txt";

//

// You can add as many of these as you wish to exclude
// - This is case sensitive -

//
$Exclude_Extension[] = "hide_me";



//

// ***-----  Do not change below this line -----*** \\

//


// include functions
include $HomeDir."functions.php";


//Path to themes folder with ending slash
$ThemeURL = $HomeURL."themes/";


// Path to icon folder with ending slash
$iconfolder = $HomeURL."icons/";


$_GLOBAL['image'] = "";


$fdir=$_GET["fdir"];
$NumSort=$_GET["NumSort"];
$SortBy=$_GET["SortBy"];
// Open folder directory

if(!isset($fdir)) 

    $fdir = "./";
 

$fdir = str_replace("../", "", $fdir);


// check to see if still inside directory boundry

$check = substr($fdir, 0, 2);
if($check != "./") 
    $fdir = "./";


// setup file properties class

class File_Properties

    
  var $file_name;       
// just the file name

  var $file_ext;        
// file extension
    
var $file_size;     
// size of file
    
var $file_date;     
// date modified
    
var $file_icon;     
// icon for file type
    
var $file_type;     
// short description for file type

    
// constructor method - build object
    
function Build($file)
    
    
    $this->setFname($file);
    
    $this->setFext($file);
    
    $this->setFsize($file);
    
    $this->setFdate($file);
    
    $this->setFicon_type();
    


    
// Set file name

function setFname($file)

    
    
    $this->file_name = basename($file);

    
    
// set file extension
    
function setFext($file)
    
    
    $this->file_ext = array_pop(explode('.', $file));

    
    
// set file size
    
function setFsize($file)

    
        
       $kbs = filesize($file);
    
    $units = array(' B', ' KB', ' MB', ' GB', ' TB');
        for ($i = 0; $kbs > 1024; $i++)  $kbs /= 1024; 
         $this->file_size = ((int)($kbs)).$units[$i];
    
    
// set date modified
    
function setFdate($file)

    
    date_default_timezone_set('Europe/Prague');
    $modified = filemtime($file);
    
    $this->file_date = date("d-M-Y  H:i", $modified);

    
    
// set file type
    
function setFicon_type()
    

        
list($this->file_type, $this->file_icon) = split("\?", GetExt($this->file_ext), 2);
    


    
// setup all get/return methods for class vars
    
function getFname()

    

        return $this->file_name;

    
    
function getFext()

    

        return $this->file_ext;
    
        
    
function getFsize()

    

        return $this->file_size;

    
    
function getFdate()

    

        return $this->file_date;

    
    
function getFicon()

    

        return $this->file_icon;

    
    
function getFtype()

    

        return $this->file_type;

    




// setup folder properties class
class Folder_Properties

    var $dir_name;                      // just the directory name
    var $dir_date;                      // date modified
    var $dir_icon = "folder.gif";       // icon for directory
    var $dir_type = "File Folder";      // short description for file type

    // constructor method - build object
    function Build($dir)
    
        $this->setFname($dir);
        $this->setFdate($dir);
    

    
// Set file name
    
function setFname($dir)
    
        $this->dir_name = basename($dir);
    
    
// set date modified
    function setFdate($dir)
    
        $modified = filemtime($dir);
        $this->dir_date = date("d-M-Y  H:i", $modified);
    

    // setup all get/return methods for class vars
    function getFname()
    
        return $this->dir_name;
    
    function getFdate()
    
        return $this->dir_date;
    
    function getFicon()
    
        return $this->dir_icon;
    
    function getFtype()
    
        return $this->dir_type;
    


// initialize file and folder arrays
$file_array = array();
$dir_array = array();
$Fname_array = array();
$Dname_array = array();

// open directory
$dir = opendir($fdir);

// Read files into array
while(false !== ($file = readdir($dir)))

    if($file != "." && $file != "..")
    
        $type = filetype($fdir.$file);
        $info = pathinfo($file);
        if($type != "dir")
        
            if(isset($info["extension"]))
            
                $file_extension = $info["extension"];
            
        

        if($type == "dir" && !in_array($file, $Exclude_Folder))
        
            // setup folder object
            $This_Dir = new Folder_Properties;
            $This_Dir->Build($fdir.$file);
            $dir_array[] = $This_Dir;
        
        elseif($type == "file" && !in_array($file, $Exclude_File) && !in_array($file_extension, $Exclude_Extension))
        
            // setup file object
            $This_File = new File_Properties;
            $This_File->Build($fdir.$file);
            $file_array[] = $This_File;
        
    

closedir($dir);

// Set default sort by method
if(!isset($SortBy) || $SortBy != 0 && $SortBy != 1) 
    $SortBy = 0;


// Number of the column to sort by (0-3) set default to 0
if(!isset($NumSort) || $NumSort != 0 && $NumSort != 1 && $NumSort != 2 && $NumSort != 3) 
    $NumSort = 0;


// determin object sorting methods
switch($NumSort)

    case 0;
        $Fsort_method = "file_name";
        $Dsort_method = "dir_name";
    break;
    case 1;
        $Fsort_method = "file_size";
        $Dsort_method = "dir_name";
    break;
    case 2;
        $Fsort_method = "file_type";
        $Dsort_method = "dir_name";
    break;
    case 3;
        $Fsort_method = "file_date";
        $Dsort_method = "dir_date";
    break;
    default:
        $Fsort_method = "file_name";
        $Dsort_method = "dir_name";


// object sorting functions
function ASC_sort_file_objects($a, $b)

    global $Fsort_method;
    $obj1 = strtolower($a->$Fsort_method);
    $obj2 = strtolower($b->$Fsort_method);
    if ($obj1 == $obj2) return 0;
    return ($obj1 < $obj2) ? -1 : 1;

function ASC_sort_dir_objects($a, $b)

    global $Dsort_method;
    $obj1 = strtolower($a->$Dsort_method);
    $obj2 = strtolower($b->$Dsort_method);
    if ($obj1 == $obj2) return 0;
    return ($obj1 < $obj2) ? -1 : 1;

function DESC_sort_file_objects($a, $b)

    global $Fsort_method;
    $obj1 = strtolower($a->$Fsort_method);
    $obj2 = strtolower($b->$Fsort_method);
    if ($obj1 == $obj2) return 0;
    return ($obj1 > $obj2) ? -1 : 1;

function DESC_sort_dir_objects($a, $b)

    global $Dsort_method;
    $obj1 = strtolower($a->$Dsort_method);
    $obj2 = strtolower($b->$Dsort_method);
    if ($obj1 == $obj2) return 0;
    return ($obj1 > $obj2) ? -1 : 1;


// sort ascending
if($SortBy == 0) 
    // sort arrays (ASCENDING)
    usort($file_array, 'ASC_sort_file_objects');
    usort($dir_array, 'ASC_sort_dir_objects');

    $arrow = "&#9650;";
    $SortBy = 1;

// sort descending
else 
    // sort arrays (DESCENDING)
    usort($file_array, 'DESC_sort_file_objects');
    usort($dir_array, 'DESC_sort_dir_objects');

    $arrow = "&#9660;";
    $SortBy = 0;


echo "<html>
<head>
<link rel=\"stylesheet\" type=\"text/css\" href=\"".$ThemeURL.$ThemeFolder."style.css\">
</head>
<body>
<table width=\"100%\" height=\"5%\" cellspacing=\"0\" cellpadding=\"5\">
<tr>
  <td class=\"Horni\">
<div align=\"center\"><img src=\"/stranky/dc.GIF\" alt=\"dc\" width=\"330\" height=\"50\" border=\"0\"></div>
</td>
</TR>
</TABLE>

<table class='tbl01'>

";


echo "
    <tr>
    
         <td class='td01' valign='top'>
    
            <div align='left'>
    
                <table width='100%'>
      
                        <tr>
        
                             <td class='td02'>&nbsp;</td>
        
                             <td class='td03' width='43%' align='left'> <a href='index.php?NumSort=0&SortBy=$SortBy&fdir=$fdir' class='link01'>Name $arrow</a></td>
        
                             <td class='td04'>&nbsp;</td>

                         <td class='td02'>&nbsp;</td>
        
                             <td class='td03' width='13%' align='right'><a href='index.php?NumSort=1&SortBy=$SortBy&fdir=$fdir' class='link01'>Size</a></td>
        
                             <td class='td04'>&nbsp;</td>
        
                             <td class='td02'>&nbsp;</td>
        
                             <td class='td03' width='14%' align='left'><a href='index.php?NumSort=2&SortBy=$SortBy&fdir=$fdir' class='link01'>Type</a></td>
        
                             <td class='td04'>&nbsp;</td>
        
                             <td class='td02'>&nbsp;</td>
        
                             <td class='td03' width='18%' align='right'><a href='index.php?NumSort=3&SortBy=$SortBy&fdir=$fdir' class='link01'>Date Modified</a></td>
        
                             <td class='td04'>&nbsp;</td>
        
                             <td width='20%'>&nbsp;</td>
      
                        </tr>
";


  // directory is not the base dir

if($fdir != "./") 
    
 // Make every other row a color
    
   $othernum = 1;

    
 // Get folder one level up
    
   $UpPath = dirname($fdir)."/";

    
   echo "
 <tr>
        
            <td class='td05'>&nbsp;</td>
        
            <td class='td06' width='43%' align='left'><a href='index.php?fdir=$UpPath' class='link01'><img src='".$iconfolder."levelup.gif' border='0'> Up One Level</a></td>
        
            <td class='td07'>&nbsp;</td>
        
            <td class='td05'>&nbsp;</td>
        
            <td class='td06' width='13%'>&nbsp;</td>
        
            <td class='td07'>&nbsp;</td>
        
            <td class='td05'>&nbsp;</td>
        
            <td class='td06' width='14%'>&nbsp;</td>
        
            <td class='td07'>&nbsp;</td>
        
            <td class='td05'>&nbsp;</td>
        
            <td class='td06' width='18%'>&nbsp;</td>
        
            <td class='td07'>&nbsp;</td>
        
            <td width='20%'>&nbsp;</td>
      
        </tr>";


else 
    
  $othernum = 0;
  



// alternate row counter

$count = 0;


// Output folder information

for($y = 0; $y < count($dir_array); $y++)

    
// alternate row colors
    
if($count % 2 != $othernum) 

        $special = "bgcolor='$RowColor'";

    
    else 

        $special = "";

    
    $count++;


    echo "

    <tr>

        <td class='td05' $special>&nbsp;</td>

        <td class='td06' $special width='43%' align='left'><a href=\"index.php?SortBy=".$SortBy."&fdir=".$fdir.$dir_array[$y]->getFname()."/\" class=\"link01\"><img src=\"".$iconfolder.$dir_array[$y]->getFicon()."\" border=\"0\"> ".$dir_array[$y]->getFname()."</td>
        <td class='td07' $special>&nbsp;</td>

    <td class='td05' $special>&nbsp;</td>

        <td class='td06' $special width='13%' align='right'>&nbsp;</td>

        <td class='td07' $special>&nbsp;</td>

        <td class='td05' $special>&nbsp;</td>

        <td class='td06' $special width='14%' align='left'>".$dir_array[$y]->getFtype()."</td>

        <td class='td07' $special>&nbsp;</td>
        <td class='td05' $special>&nbsp;</td>

        <td class='td06' $special width='18%' align='right'>".$dir_array[$y]->getFdate()."</td>

        <td class='td07' $special>&nbsp;</td>

        <td width='20%' $special>&nbsp;</td>

      </tr>

    ";



// output file info

for($y = 0; $y < count($file_array); $y++)
//while (list($key, $val) = each($Fname_array))


    // alternate row colors

    if($count % 2 != 0) 

        $special = "bgcolor='$RowColor'";

    

    else 

        $special = "";

    

    $count++;


    echo "

    <tr>

        <td class='td05' $special>&nbsp;</td>

        <td class='td06' $special width='43%' align='left'><a href=\"".$fdir.$file_array[$y]->getFname()."\" class=\"link01\" target=\"_blank\"><img src=\"".$iconfolder.$file_array[$y]->getFicon()."\" border=\"0\"> ".$file_array[$y]->getFname()."</td>

        <td class='td07' $special>&nbsp;</td>

    <td class='td05' $special>&nbsp;</td>

        <td class='td06' $special width='13%' align='right'>".$file_array[$y]->getFsize()."</td>

        <td class='td07' $special>&nbsp;</td>

        <td class='td05' $special>&nbsp;</td>

        <td class='td06' $special width='14%' align='left'>".$file_array[$y]->getFtype()."</td>
 
        <td class='td07' $special>&nbsp;</td>

        <td class='td05' $special>&nbsp;</td>
 
        <td class='td06' $special width='18%' align='right'>".$file_array[$y]->getFdate()."</td>

        <td class='td07' $special>&nbsp;</td>

        <td width='20%' $special>&nbsp;</td>

      </tr>

    ";





echo "

    </table>
    </div>
    </td>
  </tr>
  <tr>
    <td width='100%'>&nbsp;</td>
  </tr>
   <tr>
    <td class=\"td01\" valin=\"bottom\" align=\"center\">
        <p class=\"txt02\"> Systém datového centra vyvíjí a spravuje IS.</p> </td>
   </tr>
   <tr>
      <td class=\"td01\" valin=\"bottom\" align=\"center\"> <p class=\"txt02\"> Kontakt: Ing. Jaromír Mikulka,  <a href=\"mailto:jaromir.mikulka@unex.cz\">jaromir.mikulka@unex.cz</a>,  tel. 2526</p> </td>
   </tr>
   <tr>
    <td class=\"td01\" valin=\"bottom\" align=\"center\"><p class=\"txt02\">Powered By:  <b>TotalIndex</b></p></td>
  </tr>
</table>
</body>
</html>
";
?>```


  [1]: https://i.stack.imgur.com/BXm4n.png

【问题讨论】:

由于这个文件是直接在浏览器中打开的(地址是本地文件路径),所以没有执行PHP。您需要通过 PHP 服务器运行脚本。最好请公司里的人给你看。 该图像是显示目录内容的默认图像,no 文件已加载。您需要按照@El_Vanja 的建议使用服务器 在大多数情况下,由于缓存问题,style.css 中所做的更改不会立即加载。直接向您的 HTML 添加样式,例如 &lt;tr style="padding:10px"&gt; 不管怎样,var $file_name 是 PHP/4 的旧语法。虽然它仍然适用于 PHP/8,但它在 2005 年基本上被放弃了。无论是您的代码库还是您的文档都严重老化。 【参考方案1】:

你在双引号内使用双引号(")

你必须在双引号(")中使用单引号(')

所以你的代码应该是……

....................................

....................................
echo "<html>
<head>
<link rel='stylesheet' type='text/css' href='../your-directory/style.css'>
</head>

....................................

....................................

您还可以在 php 文件中包含外部 css 文件

<style>
<?php include 'CSS/main.css'; ?>
</style>

【讨论】:

谢谢,这以某种方式解决了我的问题,不知道它如何引发错误,但它可以工作,所以无论如何:D

以上是关于如何更改 PHP 文件中嵌入 HTML 的样式?的主要内容,如果未能解决你的问题,请参考以下文章

如何在单个 HTML / PHP 文件中嵌入图像?

如何替换 mailchimp CSS 嵌入代码中的按钮样式?

如何将音频文件嵌入 php 网站

如何将样式(css + html)与php隔离

如何使用 php/Mysql 动态更改 CSS 样式。我想创建每个用户独有的主题?

如何覆盖嵌入推文的样式表