B4J Library [BANanoPHP] A collection of some inline PHP functions

Ola

Download

With intentions to do some PHP related functions, i've thought why not make a class with all functions that could be used. Besides uploading files, sending emails, we can do other things too. This is based on some stuff I have been using..

Will sectionalize the examples... and all follow the INLINE PHP approach.

In AppStart, ensure that your PHP settings are done properly..

B4X:
'set php settings
    BANano.PHP_NAME = $"${AppName}.php"$
    BANano.PHPHost = $"http://${ServerIP}:${Port}/${AppName}/"$
    BANano.PHPAddHeader("Access-Control-Allow-Origin: *")

We will keep the list updated as we find more stuff..
 
Last edited:

Mashiane

Expert
Licensed User
Longtime User
1. Getting a list of files and directories from a folder.

We are using the root folder of where our app is published on..

B4X:
Dim bPHP As BANanoPHP
    bPHP.Initialize
    'get directory listing
    Dim assetsList As String = BANano.CallInlinePHPWait(bPHP.DIRECTORY_LIST, bPHP.BuildDirectoryList("./"))
    Dim dl As BANAnoPHPDirList = bPHP.GetDirectoryList(assetsList)
    BANano.GetElement("body").Append(assetsList)
    Log(dl)

bananophpdemo.png
 

Mashiane

Expert
Licensed User
Longtime User
3. Creating a log file. Text you write to the file is appended. The log in this case sits on the root folder

B4X:
Dim logFile As String = "./phplog.txt"
    
    BANano.CallInlinePHPWait(bPHP.FILE_LOG, bPHP.BuildWriteFile(logFile, "Started BANanoPHP"))
    BANano.CallInlinePHPWait(bPHP.FILE_LOG, bPHP.BuildWriteFile(logFile, "Created mashy.txt"))

Example output

B4X:
2020-06-12 21:34:51 Started BANanoPHP
2020-06-12 21:34:51 Created mashy.txt
2020-06-12 21:34:52 Get file directory listing for root
2020-06-12 21:34:52 Check file existence for mashy.txt
2020-06-12 21:34:52 Read file contents for mashy.txt
2020-06-12 21:34:52 Append to file mashy.txt
2020-06-12 21:43:33 Started BANanoPHP
2020-06-12 21:43:33 Created mashy.txt
2020-06-12 21:43:33 Get file directory listing for root
2020-06-12 21:43:33 Check file existence for mashy.txt
2020-06-12 21:43:33 Read file contents for mashy.txt
2020-06-12 21:43:33 Append to file mashy.txt
 

Mashiane

Expert
Licensed User
Longtime User
7. Copy a file from one location to another

We also call file exists to check if the file exists after being copied..

B4X:
'copy a file
    BANano.CallInlinePHPWait(bPHP.FILE_LOG, bPHP.BuildWriteFile(logFile, "Copy mashy.txt"))
    BANano.CallInlinePHPWait(bPHP.FILE_COPY, bPHP.BuildFileCopy("./mashy.txt", "./assets/mashy1.txt"))
    Dim sfc As String = BANano.CallInlinePHPWait(bPHP.FILE_EXISTS, bPHP.BuildFileExists("./assets/mashy1.txt"))
    body.Append($"File Copy Exists: ${sfc}"$)
    body.Append("<br>")
 

Mashiane

Expert
Licensed User
Longtime User
8. Rename a file

We also run file exists to see if the rename was a success

B4X:
'rename a file
    BANano.CallInlinePHPWait(bPHP.FILE_LOG, bPHP.BuildWriteFile(logFile, "Rename mashy1.txt"))
    BANano.CallInlinePHPWait(bPHP.FILE_RENAME, bPHP.BuildFileRename("./assets/mashy1.txt", "./assets/mashy2.txt"))
    Dim sfc As String = BANano.CallInlinePHPWait(bPHP.FILE_EXISTS, bPHP.BuildFileExists("./assets/mashy2.txt"))
    body.Append($"File Rename Exists: ${sfc}"$)
    body.Append("<br>")
 

Mashiane

Expert
Licensed User
Longtime User
9. Delete a file (this does not work on a directory)

We also run file exists to see if the deletion of the file worked.

B4X:
'delete a file
    BANano.CallInlinePHPWait(bPHP.FILE_LOG, bPHP.BuildWriteFile(logFile, "Delete mashy2.txt"))
    BANano.CallInlinePHPWait(bPHP.FILE_DELETE, bPHP.BuildFileDelete("./assets/mashy2.txt"))
    Dim sfc As String = BANano.CallInlinePHPWait(bPHP.FILE_EXISTS, bPHP.BuildFileExists("./assets/mashy2.txt"))
    body.Append($"File Deleted Exists: ${sfc}"$)
    body.Append("<br>")
 

Mashiane

Expert
Licensed User
Longtime User
10. Create / Make Directories - this creates directories recursively.

B4X:
'create a directory
    BANano.CallInlinePHPWait(bPHP.FILE_LOG, bPHP.BuildWriteFile(logFile, "Create recursive directories"))
    BANano.CallInlinePHPWait(bPHP.DIRECTORY_MAKE, bPHP.BuildDirectoryMake("./assets/anele/mashy/mbanga/is/enjoying/BANanoPHP"))

Example output..

recursivedirectory.png
 

Mashiane

Expert
Licensed User
Longtime User
11. Get HTML content from a URL (for example when you want to parse the content etc)

B4X:
'get html from URL
    BANano.CallInlinePHPWait(bPHP.FILE_LOG, bPHP.BuildWriteFile(logFile, "Get html from url"))
    Dim html As String = BANano.CallInlinePHPWait(bPHP.FILE_GETHTML, bPHP.BuildFileGetHTML("http://www.google.com"))
    Log(html)

We have logged the html to the console.log

htmlcontent.png
 

Mashiane

Expert
Licensed User
Longtime User
12. Get JSON content from a URL. We have used a free API here to get IP of computer as shown in the internet

B4X:
'get json from URL
    BANano.CallInlinePHPWait(bPHP.FILE_LOG, bPHP.BuildWriteFile(logFile, "Get json from url"))
    Dim json As String = BANano.CallInlinePHPWait(bPHP.FILE_GETJSON, bPHP.BuildFileGetJSON("http://jsonip.com"))
    body.Append($"URL JSON: ${json}"$)
 

Mashiane

Expert
Licensed User
Longtime User
13. Recursive folder listing...

B4X:
'recursive listing
    BANano.CallInlinePHPWait(bPHP.FILE_LOG, bPHP.BuildWriteFile(logFile, "Recursive listing"))
    Dim rl As String = BANano.CallInlinePHPWait(bPHP.DIRECTORY_LISTRECURSIVE, bPHP.BuildDirectoryListRecursive("."))
    Dim lst As List = BANano.FromJson(rl)
    Log(lst)

Sample output

dirlist.png
 
Last edited:
Top