PHP include from many subfolders

Dears,

I am at very pressed situation :-(

I started to learn Javascript some months ago and I have written some pages for amusements of my invalid niece to her birthday this Friday. Separate pages are OK. But I feel calamitous that I am still unable to set php including. I have two earlier websites with php code that runs very well so I used to copy the code from those to my new index.php. Of course, I changed the name of folder and names of files. My two websites have menu and submenus, my new website has only menu without submenu, but every file has its own folder. I quickly translated it to English and made it shorter but principle is the same. I tried:

<?php
$x = isset($_GET['x']) ? $_GET['x'] : 'hello';  

if (isset($x) && preg_match('/^[a-zA-Z0-9_-]+$/', $x))  {
    if (file_exists ("../pokus/".$x.".php"))  {
        include "../pokus/".$x.".php"; 
    } else {
        include (__DIR__)."/404.php";
    }
}
?>

but allegedly the files "was not found on this server." Naturally, they are all of them uploaded to my server.

My HTML code:

<ul> 
    <li>
        <a href="/hello/"<?php echo $x == "hello" ? 'class="choice"' : '';?>>
            <img src = "pictures/hello.jpg" width = "320" height = "240"> 
        </a>
    </li>
    <li>
        <a href="/folder1/file1/" <?php echo $x == '../file1' ? 'class="choice"' : '';?>>
            <img src = "pictures/pict1-menu.jpg" width = "320" height = "240">
        </a>
    </li>
    <li>
        <a href="/folder2/file2/" <?php echo $x == '../file2' ? 'class="choice"' : '';?>>
            <img src = "pictures/pict2-menu.jpg" width = "320" height = "240">
        </a>
    </li>
    <li>
        <a href="/folder3/file3/" <?php echo $x == '../file3' ? 'class="choice"' : '';?>>
            <img src = "pictures/pict3-menu.jpg" width = "320" height = "240">
        </a>
    </li>
</ul>

At folder called pokus I have index.php and hello.php (= some sentences to her birthday). Then I have subfolder folder1 and file1.php inside, subfolder folder2 and file2.php inside etc. I am forced to use many subfolders because each subfolder contains many pictures A1.jpg, A2.jpg ... D4.jpg using JS FOR cycle and therefore it is unable to mix them together.

I am very astonished that not even file hello.php sitting beside index.php I am not able to load. My Apache says that this file "was not found on this server." But the file hello.php is loaded immediately with opening index.php! Why is Apache so paranoid?

Here is live illustration: http://pokus.zlatberry.cz/

Can anyone devote your time to help me, please? Many thanks in advance!

2 answers

  • answered 2019-06-24 22:50 Karsankaka

    I think the issue is you are having the folder names which is good but your links are to hello and not to hello.php

    If you give the php extension it does show the file properly.

  • answered 2019-06-24 23:27 Marie-Eva BB Volmar

    Something I have realized when programming : if it's too complicated or complex it means the logic must be changed. I suggest organizing your chaos so that it's easier to debug.

    Try this for file exists :

    $filename = "../pokus/".$x.".php";
    
    if (file_exists($filename)) {
        include ($filename);
    } else {
        include (__DIR__)."/404.php";
    }
    

    Other than this, try break your codes in small chunks. Lots of ternary operators. Are they necessary? If all your files are present in said folders, the program is sending you a 404 because it is not understanding your logic. I'd say for the sake of debugging try with a different logic. Go back to the basics. Simplicity often works well.

    Example :

    if(isset($_GET['x'])){
     $x = $_GET['x'];
    }...etc.