Android Tutorial Getting List of Files of a webserver directory via httputils2 & php

When you like to download files with httputils2 you need to know the files by name. There are some examples using ftp or a generated xml file with the contents first. To me this could be easier and more dynamic.

My tutorial uses this php script:

B4X:
<?php
$return_array = array();

if ($handle = opendir('.')) {
  

    while (false !== ($file = readdir($handle))) {
        if($file == "." or $file == "..")
        {
        }
        else
        {
            $return_array[] = $file;
        }
    }
    echo json_encode($return_array);
    closedir($handle);
}
?>

As it uses "opendir('.') it will read all files of the directory/path it is started from. F.e. if you put it in www.yourdomain.com/pictures it will give back all the files in this directory as a JSON encoded array.

In b4a we use it this way:

B4X:
Dim fi As HttpJob
fi.Initialize("getfiles", Me)
fi.Download2("http://www.yourdomain.com/pictures/files.php", _
                    Array As String("Null", "Null"))

Coming back from the script:

B4X:
Sub JobDone(Job As HttpJob)
   
    If Job.Success Then
        Dim res As String
        res = Job.GetString
        Log("Back from Job:" & Job.JobName )
        Log("Response from server: " & res)
      
        Dim parser As JSONParser
        parser.Initialize(res)
      
        Select Job.JobName
      
            Case "getfiles"
                Dim ListOfFiles As List
                ListOfFiles = parser.NextArray
                If ListOfFiles.Size > 0 Then
                  For i = 0 To ListOfFiles.Size - 1
                      Log(">> File: " & ListOfFiles.Get(i))
                      'here you could start another job to download the files/images/etc.
                  Next
                End If
            End Select
      Else
        ToastMessageShow("Error: " & Job.ErrorMessage, False)
              
    End If
    Job.Release

This code is very simple and effective without any passwords or other user data.

You can call the php script from a browser, too. Just type www.yourdomain.com/pictures/files.php
 

KMatle

Expert
Licensed User
Longtime User
You can add a loop over "$return_array[]" to get the single filename and get the date & time of it with "filemtime($filename)" (take google to se details how to use it). Then you have to format the JSON a bit different. Will provide an example the next days.
 

ilan

Expert
Licensed User
Longtime User
i solved it like this:

B4X:
<?php
$return_array = array();

if ($handle = opendir('.')) {


    while (false !== ($file = readdir($handle))) {
        if($file == "." or $file == "..")
        {
        }
        else
        {
            $return_array[] = $file  . "|" . date ("d-m-Y|H:i:s", filemtime($file));
        }
    }
    echo json_encode($return_array);
    closedir($handle);
}
?>

now i am getting the filename and the date+time (now i can split the date and file with regex.split("\|",listget(i))

thanx a lot !!! :)
 
Last edited:

andyr00d

Member
Licensed User
Longtime User
Hello,

Nice script! I dropped the php example you gave at the top in a directory of files and accessed it, and it's done what you said... just one thing - can we somehow omit the file itself from being included? as it's returning:

["01.PNG","06.PNG","07.PNG","08.PNG","09.PNG","filelist.php","03.PNG","05.PNG","02.PNG","04.PNG"]

I guess once the array is loaded, we can remove the string from there?

Thanks!
 

KMatle

Expert
Licensed User
Longtime User
This line is a filter for unwanted entries ("." or ".." is for directory up or root directory). Add what you want. In Job.Done you can check the filenames, too (if you app gets more complex and you have several file types)

This is good when you know the full filename. Use php substring functions if you want something like *.jpg or *.png

B4X:
if($file == "." or $file == ".." or $file == "filelist.php"))
 

DonManfred

Expert
Licensed User
Longtime User
Top