Forum for discussing general topics related to Couch.
5 posts Page 1 of 1
Hi there, I had a quick question of sorts.
I was looking at this tutorial(http://tutorialzine.com/2014/09/cute-fi ... -ajax-php/), and thought it would be a good fit for a site I'm creating. The site has a resources section, with folders, sub-folders and eventually files to download (using dynamic folders).
The php file in the tutorial is designed to scan the folder structure, and replicate it.
I was wondering if someone with a better grasp of listing folders and files in couch would have an idea of how to replace this code, with something listing all the dynamic folders and files using couch?
Code: Select all
<?php

$dir = "files";

// Run the recursive function

$response = scan($dir);


// This function scans the files folder recursively, and builds a large array

function scan($dir){

   $files = array();

   // Is there actually such a folder/file?

   if(file_exists($dir)){
   
      foreach(scandir($dir) as $f) {
      
         if(!$f || $f[0] == '.') {
            continue; // Ignore hidden files
         }

         if(is_dir($dir . '/' . $f)) {

            // The path is a folder

            $files[] = array(
               "name" => $f,
               "type" => "folder",
               "path" => $dir . '/' . $f,
               "items" => scan($dir . '/' . $f) // Recursively get the contents of the folder
            );
         }
         
         else {

            // It is a file

            $files[] = array(
               "name" => $f,
               "type" => "file",
               "path" => $dir . '/' . $f,
               "size" => filesize($dir . '/' . $f) // Gets the size of this file
            );
         }
      }
   
   }

   return $files;
}



// Output the directory listing as JSON

header('Content-type: application/json');

echo json_encode(array(
   "name" => "files",
   "type" => "folder",
   "path" => $dir,
   "items" => $response
));

I only had a very quick go at it, but I just couldn't get my head round what was required to list the folders and their contents in a way that would work with this format.
No worries if no-one can be bothered, I realise it's a bit cheeky to come on here without giving it a serious go, but I doubt I could do it, and someone might have done something similar!
Cheers!
Hi Ewan,

The script you mentioned scans and displays the actual physical folders and files present on the server. Am I right in understanding that you want a similar script that does the same for the 'virtual' folders and cloned pages we create in Couch?

I suppose what appeals to you about the script is the way the result is presented (http://demo.tutorialzine.com/2014/09/cu ... -ajax-php/). That is all JQuery/CSS and can be used verbatim. We'd only need a Couch script that loops through the folders and pages and outputs the same JSON format.

Is that what you want?
Please let me know.
Hi KK,
That's exactly right. I just remember last time I tried to list folders/subfolders and their contents like that I just couldn't get it to work properly, I think I gave up and tried something else.
So yes, that's exactly what I'm after,
KK wrote: a Couch script that loops through the folders and pages and outputs the same JSON format.

If you have a quick solution that would be great!
Cheers!
I'll be happy to code the script for you, Ewan, but you'll have to give me a little time.
I'm currently in the thick of something important. I'll do this as soon as I get a breather.

Hope that is ok.
Of course KK, there's no rush.
Whenever you have time would be great, but don't worry if you've got too much on :)
Thanks as always,
5 posts Page 1 of 1
cron