0

I'm trying to get number of files in folder and compare it with maxfiles

I have function in dropzone.js

 Dropzone.prototype._updateMaxFilesReachedClass = function() {

      if ((this.options.maxFiles != null) && this.getAcceptedFiles().length >= this.options.maxFiles) {
        if (this.getAcceptedFiles().length === this.options.maxFiles) {
          this.emit('maxfilesreached', this.files);
        }
        return this.element.classList.add("dz-max-files-reached");
      } else {
        return this.element.classList.remove("dz-max-files-reached");
      }
    };

I would like to put number of files instead this.getAcceptedFiles().length

I tried whith this:

   $.get('handler.php?action=count' , function(data) {return data;}) === maxFiles)

But I do not know how to return number if it is possible.

file handler.php?action=count return number but I have bo idea how can get this number and use with dropzone function.

Szmulik
  • 241
  • 1
  • 4
  • 10

1 Answers1

1

I used to use php previously to get number of files and folders , but i found that ajax is able to do that simply.

This is a typical response from a real project when i sent a jquery get request to some folder that lives on my server

Request:

$.ajax({
  url: "projects/"+current_project+"/img/" 

})

Response

"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
 <head>
  <title>Index of /dumar/projects/dumar/img</title>
 </head>
 <body>
<h1>Index of /dumar/projects/dumar/img</h1>
  <table>
   <tr><th valign="top"><img src="/icons/blank.gif" alt="[ICO]"></th><th><a href="?C=N;O=D">Name</a></th><th><a href="?C=M;O=A">Last modified</a></th><th><a href="?C=S;O=A">Size</a></th><th><a href="?C=D;O=A">Description</a></th></tr>
   <tr><th colspan="5"><hr></th></tr>
<tr><td valign="top"><img src="/icons/back.gif" alt="[PARENTDIR]"></td><td><a href="/dumar/projects/dumar/">Parent Directory</a>       </td><td>&nbsp;</td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/image2.gif" alt="[IMG]"></td><td><a href="dumar1.jpg">dumar1.jpg</a>             </td><td align="right">2015-01-21 01:04  </td><td align="right">174K</td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/image2.gif" alt="[IMG]"></td><td><a href="dumar2.jpg">dumar2.jpg</a>             </td><td align="right">2015-01-21 01:05  </td><td align="right">177K</td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/image2.gif" alt="[IMG]"></td><td><a href="dumar3.jpg">dumar3.jpg</a>             </td><td align="right">2015-01-21 01:06  </td><td align="right">117K</td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/image2.gif" alt="[IMG]"></td><td><a href="dumar4.jpg">dumar4.jpg</a>             </td><td align="right">2015-01-21 01:06  </td><td align="right">110K</td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/image2.gif" alt="[IMG]"></td><td><a href="dumar5.jpg">dumar5.jpg</a>             </td><td align="right">2015-01-21 01:19  </td><td align="right">118K</td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/image2.gif" alt="[IMG]"></td><td><a href="dumar6.jpg">dumar6.jpg</a>             </td><td align="right">2015-01-21 01:19  </td><td align="right">127K</td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/image2.gif" alt="[IMG]"></td><td><a href="dumar7.jpg">dumar7.jpg</a>             </td><td align="right">2015-01-21 01:20  </td><td align="right">137K</td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/image2.gif" alt="[IMG]"></td><td><a href="dumar8.jpg">dumar8.jpg</a>             </td><td align="right">2015-01-21 01:20  </td><td align="right">121K</td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/image2.gif" alt="[IMG]"></td><td><a href="dumar9.jpg">dumar9.jpg</a>             </td><td align="right">2015-01-21 01:39  </td><td align="right"> 88K</td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/image2.gif" alt="[IMG]"></td><td><a href="dumar10.jpg">dumar10.jpg</a>            </td><td align="right">2015-01-21 01:34  </td><td align="right"> 79K</td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/image2.gif" alt="[IMG]"></td><td><a href="dumar11.jpg">dumar11.jpg</a>            </td><td align="right">2015-01-21 01:35  </td><td align="right"> 78K</td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/image2.gif" alt="[IMG]"></td><td><a href="dumar12.jpg">dumar12.jpg</a>            </td><td align="right">2015-01-21 01:38  </td><td align="right"> 75K</td><td>&nbsp;</td></tr>
   <tr><th colspan="5"><hr></th></tr>
</table>
<address>Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.5.15 Server at 127.0.0.1 Port 80</address>
</body></html>

if you parse this html , you will find it looks like a file explorer enter image description here

the first two table rows , are the table header and a horizontal seperator , while last table row are a horizontal seperator.

You can simply count rows and mainpulate your html like if you with any html page.

for sake of illustration , I used this script to count files in that directory (only files ):

$.ajax({
  url: "projects/"+current_project+"/img/" 

})
  .done(function( data ) {
    $(".project-gallery-thumbs").empty();        
    $(data).find("tbody tr a").each(function(i){
        if(i>=5)
        {

            $(".project-gallery-thumbs").append("<div class='thumb-item' style='background-image:url( projects/"+current_project+"/img/"+$(this).text()+")' data-thumb='"+$(this).text()+"'></div>")

        }

    });

Solution:

In your case you can use same algorithm , but instead count links starting from fourth one (ignore table header links) , something like:

$.ajax({
  url: "your folder here"  

})
  .done(function( data ) {      
    $(data).find("tbody tr a").each(function(i){
        if(i>=4)
        {           
            //this will iterate all folders and files found there           
        }

    });
ProllyGeek
  • 15,517
  • 9
  • 53
  • 72