0

How can I write this PHP code in JavaScript?

I have no knowledge about JavaScript... :-s

<?php
    $pozethumb=scandir("./pics/flori/thumbs");
    $size=count($pozethumb);
    $nrpoze=$size-2;                    
    for($i=2;$i<$size;$i++)
    {
        echo"<img src=\"./pics/flori/thumbs/$pozethumb[$i]\" class=\"thumb\" sou=\"$pozethumb[$i]\" />";
    }                    
?>
2
  • 2
    PHP is a server-side language, JavaScript usually a client-side one. Client-side JS has no possibility of listing files in a folder. How do you expect this to work? Or do you mean server-side Javascript? Commented Nov 2, 2010 at 10:42
  • well i said i have no knowledge about javascript, i thought it could be possible to do this. thnx. i'll modify the php code to solve my problem. Commented Nov 2, 2010 at 10:46

2 Answers 2

4

Consider familiarizing yourself with nodejs. It requires you to have a serverside V8 JS engine installed on the server. This would allow you to use ECMAScript/JavaScript on the server, e.g. list files on the filesystem of the server and query it from the client.

See the API docs

  • fs.readdir(path, [callback])
    Asynchronous readdir(3). Reads the contents of a directory. The callback gets two arguments (err, files) where files is an array of the names of the files in the directory excluding '.' and '..'.

and

  • fs.readdirSync(path)
    Synchronous readdir(3). Returns an array of filenames excluding '.' and '..'.

You might also be interested in phpjs, which aims to port PHP functions to PHP (doesnt have scandir though and I'm really not sure what to think of that in general anyway).

Sign up to request clarification or add additional context in comments.

Comments

3

You can't read files from the server's file system using JavaScript.

Javascript runs on the browser, so you don't have access to server, unless you write some PHP code and AJAX to do that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.