dtoqa66028 2015-06-19 03:53
浏览 205
已采纳

在PHP中使用函数添加CSS

I was try to add CSS file to my html file using statistic function like this:

      public static function addCSS($file){
         $cssPath = $_SERVER['DOCUMENT_ROOT']. DIRECTORY_SEPARATOR. 'new'. 
         DIRECTORY_SEPARATOR. 'css/'.$file;
           return file_exists($cssPath) ? "
   <link rel=\"stylesheet\" href=\"$cssPath\" type=\"text/css\" 
    media=\"screen\" charset=\"utf-8\" />
    " : "CSS File not found";
        }

but it doesn't work as i expected. i want it to produce the CSSPath like "http:localhost/new/css/admin.css when I'm called using General::addCSS('admin.css'); but all i got is address like this which make me fail to include: C:/xampp/htdocs ew\css/admin.css. how can i fix this addressing?

  • 写回答

4条回答 默认 最新

  • doulieyu0881 2015-06-19 04:06
    关注

    You are using xampp, so the document root path will be from that "Server". Thats why the path is wrong. you will need to exclusively specify what path you want, until you move to your server.

    Also, if you are using a shared hosting plan, document root will not work most of the time. Set up a Constant with the server root path so that you can use it just like you use DOCUMENT_ROOT.

         //Create root path
         define('ROOT', 'something/anotherthing/public_html');
    
         public static function addCSS($file){
         //use root path everywhere instead of document_root
         $cssPath = ROOT . DIRECTORY_SEPARATOR. 'new'. 
         DIRECTORY_SEPARATOR. 'css/'.$file;
           return file_exists($cssPath) ? "
         <link rel=\"stylesheet\" href=\"$cssPath\" type=\"text/css\" media=\"screen\" charset=\"utf-8\" />
         " : "CSS File not found";
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?