I have a js code to include a div from one mywebsite (A) to another one (B).
In (A) php code I set a variable $baseurl=“$thatwebsite/path-in-that-website/”, in this way I have the same code for local and remote (too long to explain here, unless you ask). And in a php page of (A) I have a div (with id="someid) that contains books of a given writer, linked using as base the above variable ($baseurl) + what is specific of that link.
In the footer of (B) I call a js in this structure:
Today I have rewritten the php code to automatize files listed and linked, and that code works only in (A), non more in (B). because in (B) the variable $thatwebsite is not recognized, and the path is wholly related to (B).
The variable $thatwebsite is defined in the php pages of (B) (in the head of each page), the js script is at the bottom of each page.
Should I provide further info?
I’m not sure but it seems a js problem, and not a php one: I’m wrong?
And what should I do?
Thank you!
Aside from the bizarre quotes in this line below (I’d remove the quotes around $thatwebsite), I don’t see the issue. What happens if you echo out $thatwebsite from php, just above this line but within the html? Does it look good there?
Indeed, probably the problem is php, and not js. Should I change something in the collocation of this thread?
The php code is:
<?php
// Files to exclude
$excludedFiles = ['somefile'];
function listFilesAndFolders($dir, $excludedFiles, $level = 3) {//level = 3 dice che si parte da h3, se fosse 2 partirebbe da h2
// Open the directory
if ($handle = opendir($dir)) {
// Loop through the directory
while (false !== ($entry = readdir($handle))) {
// Skip the current and parent directory entries
if ($entry != "." && $entry != "..") {
$fullPath = "$dir/$entry";
// Check if it's a directory
if (is_dir($fullPath)) {
// Display the directory name as a heading
echo "<h$level><a href=\"$fullPath\">$entry</a></h$level><ul>";
// Recursively list files in the subdirectory, increasing heading level
listFilesAndFolders($fullPath, $excludedFiles, $level + 1);
} else {
// Check if the file should be excluded
if (!in_array($entry, $excludedFiles)) {
$entry2 = str_replace('-', ' ', $entry);
$entry2 = pathinfo($entry2, PATHINFO_FILENAME);
// It's a file, display the file name as a link
echo "<li><a href=\"$fullPath\">$entry2</a></li>\n";
}
}
}
}
echo "</ul>";
closedir($handle);
}
}
// Call the function to start listing from the specified path
listFilesAndFolders($path, $excludedFiles);
?>
The above script has this strange (in my opinion, sorry, if I’m wrong) behavior:
while this below provide the right path: echo $mypath;
this other, instead, doesn’t work (no output at all): $path = "$mypath/dir1/dir2/dir3/";
But this works (the php page is in the folder dir2):
$path = "./dir3/";
I guess that the problem is opendir. I need that its value is not the current dir, but one set by a variable.
The strange is that php says: Failed to open directory: https://localhost/[my-right-path]
even if that path is existing …
EDIT
Doing this test:
<?php
$testDir = 'https://localhost/my-absolute-path/'; // Use your directory path here
if (is_dir($testDir)) {
if ($handle = opendir($testDir)) {
echo "Successfully opened directory: $testDir";
closedir($handle);
} else {
echo "Failed to open directory: $testDir";
}
} else {
echo "The path does not exist or is not a directory: $testDir";
}
?>
There may be more than one problem here. Given the definition of opendir(), you can’t put a URL in as the first parameter. It’s expecting a directory string, ie no https:// domain/ stuff. Same with is_dir().
I tried with glob, but unsuccessfully. And inexplicably.
<?php
$path = trim("$baseurl$lastpath");
// Files to exclude
$excludedFiles = ['normal.inc', 'index.php'];
function listFilesAndFolders($dir, $excludedFiles, $level = 3) {
// Use glob to find files and directories
$entries = glob("$dir/*");
if ($entries) {
echo "<p>Successfully opened directory: $dir</p>";
$hasEntries = false; // Flag to check if there are entries
foreach ($entries as $entry) {
$entryName = basename($entry);
// Skip excluded files
if (in_array($entryName, $excludedFiles)) {
continue;
}
$hasEntries = true; // There are valid entries
// Check if it's a directory
if (is_dir($entry)) {
echo "<h$level><a href=\"$entry\">$entryName</a></h$level><ul>";
// Recursively list files in the subdirectory, increasing heading level
listFilesAndFolders($entry, $excludedFiles, $level + 1);
echo "</ul>"; // Closing the <ul> after the recursive call
} else {
// Optionally, you can display files as well
echo "<li>$entryName</li>";
}
}
if (!$hasEntries) {
echo "<p>No files or folders found in $dir.</p>";
}
} else {
echo "<p>Failed to open directory: $dir</p>";
}
}
// Call the function to start listing from the specified path
listFilesAndFolders($path, $excludedFiles);
?>
I don’t know of a function to allow http access to a directory on a remote server. As you can imagine, it’s a security concern. But look into the ftp functions, e.g. ftp_nlist() which allows it assuming you provide login credentials to the remote machine.
Thank you! This morning I had this idea: the problem could be a variable, defined (in another file, but I guess this is not relevant) within an if statement, called within another if statement.
So, a nested if, I suppose, could be the php problem.
So far I haven’t said, but $thatwebsite is defined within an if statement:
<?php
$excludedFiles = ['normal.inc', 'index.php'];
function listFilesAndFolders($path, $excludedFiles, $baseurl, $level = 3) {
// Use glob to find files and directories
$entries = glob("$path/*");
if ($entries) {
$hasEntries = false; // Flag to check if there are entries
foreach ($entries as $entry) {
$entryName = basename($entry);
$wholehrefpath = "$baseurl/$path/$entryName";
// Skip excluded files
if (in_array($entryName, $excludedFiles)) {
continue;
}
$hasEntries = true; // There are valid entries
// Check if it's a directory
if (is_dir($entry)) {
echo "<h$level><a href=\"$entry\">$entryName</a></h$level><ul>";
// Recursively list files in the subdirectory, increasing heading level
listFilesAndFolders($entry, $excludedFiles, $baseurl, $level + 1);
echo "</ul>"; // Closing the <ul> after the recursive call
} else {
// Link each file
echo "<li><a href=\"$wholehrefpath\">$entryName</a></li>";
}
}
if (!$hasEntries) {
echo "<p>No files or folders found in $path.</p>";
}
} else {
echo "<p>Failed to open directory: $path</p>";
}
}
// Call the function to start listing from the specified path
listFilesAndFolders($path, $excludedFiles, $baseurl);
?>
Or better, it works only once: only for a writer
I can call this working:
<?php
$path = "./writer1";
include "$root/list-files-in-subfolders.inc";
?>
Surely you only need to include it once, and just keep calling the function for each value of $path? Don’t call it in your included file, just include the file at the start and call the function each time you need to.
When you say “it works only for the first writer”, what happens for the second and subsequent ones?
I don’t see where you call your listFilesAndFolders() function for the second value of $path.
I still don’t know what you mean by “doesn’t work”. Does it do nothing? That wouldn’t be surprising if you’re not calling the function a second time. Or does it give you an error message?
(Note - I haven’t looked at the contents of the function, I’m just talking about how you need to call it more than once.)
Doesn’t work means: no output.
But at the end, I decide to put that script splitting writer pages in separated directories. And so, all work as expected.
no error messages. I don’t have duplicated the script: all the php pages in several directories use the same script.
I’m not an expert of php, and I guess that is a no-perfect workaround. But for my needs it’s enough, at the present.