在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?
我尝试将 但它无法正常工作。 当我使用
CSS code>文件添加到我的
html code> 使用统计函数的文件如下: p>
public static function addCSS($ file){
$ cssPath = $ _SERVER ['DOCUMENT_ROOT']。 DIRECTORY_SEPARATOR。 '新'。
DIRECTORY_SEPARATOR。 'css /'.$文件;
返回file_exists($ cssPath)? “
&lt; link rel = \”stylesheet \“href = \”$ cssPath \“type = \”text / css \“
media = \”screen \“charset = \”utf-8 \“/&gt ;
“:”未找到CSS文件“;
}
code> pre>
General :: addCSS('admin.css'); code>调用时,我希望它生成类似“http:localhost / new / css / admin.css”的CSSPath但是所有我 得到的是这样的地址,这使我无法包括:C:/ xampp / htdocs
ew \ css / admin.css。我该如何修复此寻址? p>
div>
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";
}
$_SERVER['DOCUMENT_ROOT']
, returns just that, a path to a directory and not a URL. If you want the URL to your site, try using:
"http://{$HTTP_HOST}{$REQUEST_URI}"
So, in your case it will be:
$REQUEST_URI= 'css/admin.css'
$cssPath = "http://{$HTTP_HOST}{$REQUEST_URI}";
You cannot use $_SERVER['DOCUMENT_ROOT']
for $cssPath
because it will be a system filepath, you need a url. If you really want to implement this feature this way try:
public static function addCSS( $file ){
$css_dir = DS . 'new'. DS . 'css'. DS;
$css_file = $_SERVER['SERVER_NAME'] . $css_dir . $file;
$css_file_path = $_SERVER['DOCUMENT_ROOT'] . $css_dir . $file;
return file_exists( $css_file_path ) ? '<link rel="stylesheet" href="'.$css_file.'" type="text/css" media="screen" />' : false;
}
My advice however is to reconsider your approach to the problem.
DOCUMENT_ROOT
will return simply the document path , But to include css you need an absolute or relative urls.You can try with $_SERVER['HTTP_HOST']
public static function addCSS( $file ){
$css_dir = DS . 'new'. DS . 'css'. DS;
$css_file = 'http://'.$_SERVER['HTTP_HOST'] . $css_dir . $file;
$css_file_path = $_SERVER['DOCUMENT_ROOT'] . $css_dir . $file;
return file_exists( $css_file_path ) ? '<link rel="stylesheet" href="'.$css_file.'" type="text/css" media="screen" />' : false;
}