JamesPy JamesPy - 2 months ago 21
Apache Configuration Question

apache php javascript - Failed to open stream: No such file or directory in /var/www/html/bla

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);
}
?>

Answer

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.