1

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...)

Rahul-2001
  • 11
  • 2
  • Provide absolute path from root instead of relative path. – sandeepsure Oct 24 '15 at 18:40
  • @sandeepsure can you elaborate?? I'm new to PHP – Rahul-2001 Oct 24 '15 at 18:41
  • Read this http://stackoverflow.com/questions/21306512/difference-between-relative-path-and-absolute-path-in-javascript – sandeepsure Oct 24 '15 at 18:43
  • Example for linux absolute path can go smth like that: '/var/www/html/file.php', and for windows 'C:\\wamp\www\file.php' . Open command line and drag&drop file into it, it will show absolute path of file. – xReprisal Oct 24 '15 at 18:47
  • @ArekGorecki I do not have physical access to the server... will it be `root/secure`? – Rahul-2001 Oct 25 '15 at 13:18
  • I think @Fred got explained pretty well whole idea, but it should be under /root/secure – xReprisal Oct 25 '15 at 13:26
  • @Rahul-2001 if you're able to do `include("../../secure.html");` that is in your root, then you can use `include("../../folder/secure.html");` as outlined in my answer along with a few other examples. – Funk Forty Niner Oct 25 '15 at 13:28

2 Answers2

1

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:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

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!

Community
  • 1
  • 1
zuko32
  • 239
  • 1
  • 9