开发者

Including a file via variable

开发者 https://www.devze.com 2023-01-09 08:10 出处:网络
This seems like the most basic thing in the world, but I don\'t know PHP. I am trying to include a file on my page, and the directory of that file is specified inside of a variable.

This seems like the most basic thing in the world, but I don't know PHP.

I am trying to include a file on my page, and the directory of that file is specified inside of a variable.

<?php
    $pageGroup = 'news';
    $directory = 'http://localhost:8888/includes/'.$pageGroup.'/rightCol.html';
    include($directory);
?>

When I echo the results of $directory, I get "http://localhost:8888/includes/news/rightCol.html", which is exactly right. Yet when I try to include the file at that directory, nothing happens. I am 开发者_如何转开发assuming this has something to do with the include syntax.

Any suggestions are appreciated.


Make sure that you have allow_url_fopen settings turned on from php.ini.

Having said that, there are security risks attached to that, see:

  • allow_url_fopen Security

Suggestion:

To include files, don't use complete url path, use just file/folder paths.

Example:

include "/includes/news/rightCol.html"; // get from specified dir
include "../rightCol.html"; // get from parent dir
include "../../rightCol.html"; // get from parent parent dir

To include files from root, you might want to prefix your path with:

$_SERVER['DOCUMENT_ROOT'];


You don't want to include a http:// resource because of security concerns. Rather than you need to get it's contents via file_get_contents but if this is a simple page element you can use include without the full url.

0

精彩评论

暂无评论...
验证码 换一张
取 消