0

I am studying the php scandir method trying to return to javascript the result as an array. I read here many answers about Ajax fetch but because of my fault I didn't quite understand how to pass the headers between js and php. From php (I copied the scandir code from https://www.php.net/manual/en/function.scandir.php) 'echo json_encode ($ arrDir)' returns 'content-type = text / html' and instead of the array contents I receive the php file code as text. Can you please help me to understand what I did wrong and how can I get the array values? If possible in Vanilla JS. Thanks in advance

var arrDir = [];                            // array with directories to be returned
let url = "/testscan.php";

let response = fetch(url)
.then(response => {
    const contentType = response.headers.get('Content-Type');
    console.log("result: " + response.status + "\ntype: " + contentType);
    // iterates all headers and show 
    for (let [key, value] of response.headers) {console.log(`${key} = ${value}`);}

    if (!response.ok) {         // error
        if(response.status == 404){console.log("file does not exist")}
        throw new Error(response.status);
    }

    if (/text\/html/i.test(contentType)) {
        return response.text()
        .then(function(text) {
            console.log(text);        
        })

    } else if (/application\/json/.test(contentType)) {
        return response.json()
        .then(function(json) {
            console.log(json);        
        })
    } 
})
.catch(function (err) {             // .catch(error => {
  // do something when error happens
});

file testscan.php

$dir = "/PRODUCTS";
$arrDir = array();
Scan($dir, $arrDir);
echo json_encode($arrDir);

function Scan($dir, $arrDir){
    // Create array of current directory
    $files = scandir($dir);    
    if(is_array($files)){
        foreach($files as $val){
            // Skip home and previous listings
            if($val == '.' || $val == '..')
                continue;
           
            if(is_dir($dir.'/'.$val)){
                $arrDir[$dir][] = $val;
                Scan($dir.'/'.$val, $arrDir);
            }
            else{
                $arrDir[$dir][] = $val;
            };
        };
    };
    return $arrDir;
};
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
  • @M. Eriksson the linked question (and all the answers) are referred to LOCAL server. My problem is in remote server. Now this one is closed without any solution. What does "If this question doesn’t resolve your question, ask a new one." mean? Shall I open a new one? – mini modelli Jan 29 '22 at 11:46
  • The duplicate question isn't specific to *local* servers. – Quentin Jan 29 '22 at 12:00
  • @ Quentin- thanks for your replay, can you please tell me which comment nr. answers to my question? I read it again but I see only "Localserver, Xampp, how to configure it..." My question was: what is wrong in my Ajax call to php from index.html to file.php both in remote server, that return the html content of the file? Thanks again if you give me some indication about. Consider that I am a 75 y.o. beginner ;-) – mini modelli Jan 29 '22 at 13:06
  • The accepted answer lists 6 different things that could give the symptoms you describe. (Well the last one can't). – Quentin Jan 29 '22 at 13:08
  • @ Quentin - I appreciate your answer anyway but my problem is that I don't use Apache nor Wampp, my file has the .php extension on it and I'm not using short tags in the PHP file, so it is not a duplicate. Unfortunately as the discussion is closed, no one can help me. I thought it was possible to associate a php scandir return to javascript in a dedicated question that does not seem to be a duplicate as I did not find it. The problem remains and the discussion you linked does not solve it, but if these are the rules I will look for some other solution. Thanks – mini modelli Jan 29 '22 at 15:42
  • First off, I have nothing to do with the closing this as a duplicate.. But as you say _"my problem is that I don't use Apache nor Wampp"_ which is correct, that is the problem. To be able to run the above you _need_ a web server with PHP support. PHP is a server side language, which means that it gets executed on the server while JS is a client side language that gets executed in the client (browser). If you don't have a server that can parse the PHP, then the browser will just get the source code back and show it as text. The browser has no clue what PHP is. – M. Eriksson Jan 29 '22 at 19:02

0 Answers0