Everything about Domino, PHP and other programming languages

Removing files and subdirectories from filesystem with lotusscript

Ferdi Verlaan  October 27 2014 12:09:26
We are working on a PHP / Domino hybrid mobile web application. We will post some more information about it in the near future. We need PHP since we are sending out POST commands via the HTML5 file API. A Notes Agent is not capable of understanding these requests (via the normal session.request_content for example), and therefor we need PHP. It would also be possible by xPages and JAVA I assume, but PHP easier for us.

The PHP script is called asynchronous and uploads resized images to c:\php\files\<UniqueID>. A webQuerySave agent processes the newly saved NotesDocument and fetches all documents from this subfolder and attachs them to the RichTextItem.


The cleanup of these files is done by a cleanup Agent, not in the WQS agent, to have some fallback in case of errors. We run this agent every weekend to remove all uploaded files.


Cleanup Agent code:


Option
Public
Option
Declare

Dim
globalPath As string
Sub
Initialize
       
       globalPath =
"C:\php\files\"
       
Call ScanDirs( globalPath )
End
Sub
' ==========================================================

' @FUNCTION: ScanDirs

' @AUTHOR: f.verlaan@aashq.nl
' @DATE: 27-10-2014

'

' Loops through the given path and removes all files

' and subfolders.

' based upon:
http://www-01.ibm.com/support/docview.wss?uid=swg21317698
' ==========================================================


Sub
ScanDirs(path As String)
       
       
On Error GoTo catch
       
On Error 5 Resume next
       
       
Dim sess As New NotesSession
       
Dim DirList As Variant
       
Dim filename As String
       
Dim filepath As String
       
Dim sep As String
       
If path <> "" Then

               
If InStr(sess.Platform, "Windows") > 0 Then
                       sep =
"\"
               
Else
                       sep =
"/"
               
End If
               
ReDim DirList(0)
               
If InStr(path, sep) > 0 Then
                       filepath =
StrLeftBack(path, sep)
               
End If
               filename =
Dir(path & "*", 16)
               
While filename <> ""
                       
If filename <> "." And filename <> ".." Then
                               
If (GetFileAttr(filepath & sep & filename)= 16) Then
                                       DirList =
ArrayAppend(DirList,filepath & sep & filename & sep)
                               
Else
                                       
' remove file
                                       
Kill filepath & sep & filename
                               
End If
                       
End If
                       filename =
Dir
                       ' if filename is empty and filepath is not the starting folder, delete subfolder
                       
If filename = "" Then
                               
If filepath <> StrLeftBack(globalPath, sep) Then
                                       
RmDir filepath        
                               
End If
                       
End If
                       
               
Wend
               DirList =
FullTrim(DirList)
               
               
               
ForAll dirpath In DirList
                       ScanDirs(dirpath)

               
End ForAll
       
End If
       
       
Exit sub
       
catch:

       
Print "Error " & Err & " in " & GetThreadInfo(1) & " on line " & Erl & " - " & Error
       
Exit sub
End
Sub

Make sure that the security setting for the agent is set to 3 to allow restricted operations.

    Bookmark and Share