0

I hope some bright spark can help me here. I have a site I am building, on an apache web server (Raspberry Pi 2). I am coding the site in php.

Index.php has a div that is refreshed using a simple jQuery script I wrote.

Index.php includes /include/start/times.php

times.php includes /functions/time_func.php that calls a function to display live times in the index.php div.

The jQuery refreshes every 10 seconds.

When I first load the page, the div displays correctly. When the jQuery runs 10 seconds later, the div is filled with the error in the title. I also get this error below:

Warning: include(): Failed opening 'functions/times.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear')

I have looked at server permissions, and everything else I can think of. I'm hoping someone can help me this is causing me so much frustration now.

Interestingly, if I dump all the relevant files in my website root, and modify the inclusion paths, it all works fine :-/

Here are some file contents:

index.php:

<html>
<head lang="en">  
<meta charset="UTF-8">
<link href="css/style.css" rel="stylesheet">
<script src="js/jquery-2.1.3.min.js"></script>
<script>  
function updateLeft() {
var url = "include/start/times.php";
jQuery("#left").load(url);
}
setInterval("updateLeft()", 5000);
</script>
</head>
<body style="overflow-y: hidden;">
<?php
    include 'include/start/times.php';
?>
</body>
</html>

times.php:

<div id="left">
    <p class="no-margin text-shadow">Times: <br><br><span class="text-bold" id="pressure">
    <?php 
        include 'functions/time_func.php';
        times();
    ?>
    </span></p>
</div>

time_func.php:

<?php

    // Define the cURL function
    function curl($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20160904 Firefox/4");
        $page = curl_exec($ch);
        curl_close($ch);
        return $page;
    }    

    function times(){
        $x_data = curl("http://mysite.james.com/test_sceh.html");
        $doc = new DOMDocument();
        $doc->loadHTML($x_data);
        $sec1 = $doc->getElementsByTagName('td')->item(0);
        $time1 = $doc->getElementsByTagName('td')->item(4);
        $sec2 = $doc->getElementsByTagName('td')->item(6);
        $time2 = $doc->getElementsByTagName('td')->item(10);
        $sec3 = $doc->getElementsByTagName('td')->item(12);
        $time3 = $doc->getElementsByTagName('td')->item(16);
        $xtime = $sec1->nodeValue . ' - ' . $time1->nodeValue . '<br>' . $sec2->nodeValue . ' - ' . $time2->nodeValue . '<br>' . $sec3->nodeValue . ' - ' . $time3->nodeValue . '<br>';
        print_r($xtime);         
    }
?>
  • From the error, it looks like you're trying to include a file that isn't there – adeneo Sep 22 '16 at 19:59
  • Hi Marc, It does indeed yes, however the file is there, and executes when the page is first loaded. When jQuery subsequently tries to refresh the div, it then acts like it isn't there... –  Sep 22 '16 at 20:00
  • can you post Index.php, times.php and time_func.php contents? – Pipe Sep 22 '16 at 20:02
  • Hi there, Done... –  Sep 22 '16 at 20:21
  • Initially copied an old version, refresh to see the live version. Just a few changed variable and file names... –  Sep 22 '16 at 20:28
  • Possible duplicate of [PHP - Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Sep 24 '16 at 08:40

1 Answers1

0

It happens because the paths don't get resolved the same way.

When you load the page for the first time, it all starts from index.php I guess 'functions' is at the same level as include

When your include paths are relative, they are relative to the ancestor. So, relative to index.php when you load the page, but relative to times.php when you do your ajax request.

Because functions is not a subfolder of include/start, it fails to find the time_func.php

For this kind of scenario, where the same files are included from different parent folders, you should use an absolute path.

Something like:

<?php include $_SERVER['DOCUMENT_ROOT'].'/include/start/times.php'; ?>

and

<?php include $_SERVER['DOCUMENT_ROOT'].'/functions/time_func.php'; ?>

Or course, don't change the relative path you use in var url, it needs to remain relative to the root of the hostname.

Capsule
  • 6,118
  • 1
  • 20
  • 27
  • Of course it is... I had looked at pretty much everything but this, and I feel stupid. Thanks very much for your input, that has sorted it out. –  Sep 23 '16 at 11:52
  • There is a troubleshooting checklist for this frequent error : https://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew Sep 24 '16 at 08:42