Reputation: 11
Currently the following code successfully opens secure.html
:
include("../../secure.html");
however, the following shows a 404 error:
include("../../secure");
It should be noted that the secure.html
file and the /secure
directory aren't located in public_html
but in the root
...
What am I doing wrong??
UPDATE: I want to show all the files in the directory (like apache does if there isn't an index file...)
Upvotes: 1
Views: 142
Reputation: 74217
The manual on "include" http://php.net/manual/en/function.include.php states:
"The include statement includes and evaluates the specified file.".
It makes no mention for folders.
Adding the /
at the end of "secure" include("../../secure/");
which would technically try and look for an index file, still won't work because PHP's include()
requires a specific filename.
However, if you want to include all files such as ones with an .html
extension, you can use:
foreach (glob("../../*.html") as $filename)
{
include $filename;
}
You could probably get away with something like this in order to include all files inside a given folder:
foreach (glob("../../folder/*") as $filename)
{
include $filename;
}
Or using scandir()
: and pulled from this answer on Stack https://stackoverflow.com/a/2692368/
foreach (scandir(dirname(__FILE__)) as $filename) {
$path = dirname(__FILE__) . '/' . $filename;
if (is_file($path)) {
require $path;
}
}
I don't know what the ultimate goal is for you to want and use include("../../secure");
if it's to include all files in that folder, or an index file.
References:
Having error reporting set on your system to catch and display should be throwing you something similar to:
Warning: include(/home/user/htdocs/folder): failed to open stream: No such file or directory in /home/user/htdocs/other_folder/file.php on line x
Footnotes:
You may also need to use a full system path.
I.e.:
/var/user/home/htdocs/folder/file.xxx
Testing with a folder/file in my root proved to be successful: (On a Linux box)
include("../../folder_in_root/index.html");
but not
include("../../folder_in_root");
nor
include("../../folder_in_root/");
You can also use and define a constant: (again, if you want to include the index file of that folder)
define("TEST", "../../folder_in_root/index.html");
include(TEST); // do not quote this
Note that define("TEST", "../../folder_in_root/");
will not work; it must be a filename.
Reference:
Upvotes: 1
Reputation: 239
I think that the problem here is that in the second line, correct me if I am wrong, you try to include a whole directory(i.e. all its files). However, this is not what PHP understands. What it understands is that you try to include the file that is two directories back named 'secure', just 'secure', without extension and I assume that there is no such file two directories back and that is why you get an 404. Note that files are not necessary to have an extension, so that's why you can include a file that does not have one. If what you want is to include all the files inside the directory named 'secure', I think it would be helpful to check this thread:
How to include() all PHP files from a directory?
If what you want to do is not what I described, please provide some more information! Hope it helped!
Upvotes: 0