Page 1 sur 1

Connexion FTP

Publié : mar. 26/juin/2007 18:09
par lepiaf31
Bonjour, je voudrais me connecter a mon FTP, ya t-il une librairie pour faire ca ?J'ai deja essayé la librairie FTP sur purearea, mais elle est trop ancienne et ne fonctionne plus.
Merci

Publié : mar. 26/juin/2007 19:08
par Flype
il y a deux façons de faire :

1/ passer par les fonctions windows toutes faites

2/ envoyer soi meme les commandes au serveur ftp (list, user, ...)



voici un exemple (je m'y étais essayé aussi) pour la méthode 1/
en s'aidant des fonctions windows décrites sur msdn :
http://msdn2.microsoft.com/en-us/library/aa385473.aspx

l'intérêt de cet exemple est de construire une arborescence du dossier ftp voulu ce qui implique l'utilisation d'une fonction récursive : FtpListAll().

Code : Tout sélectionner

;============================================

EnableExplicit

#FTP_TRANSFER_ASCII  = 1
#FTP_TRANSFER_BINARY = 2

#INTERNET_SERVICE_FTP = 1
#INTERNET_OPEN_TYPE_DIRECT = 1

Import "shlwapi.lib"
  StrFormatByteSize64A(Size.q, *BufStr, BufSize.l) 
EndImport

Procedure.s StrByteSize(nBytes.q)
  
  Protected string.s{64}
  
  If StrFormatByteSize64A(nBytes, @string, 64)
    ProcedureReturn PeekS(@string, -1, #PB_Ascii)
  EndIf
  
EndProcedure
Procedure.l FileIcon(Path.s)
  
  Protected info.SHFILEINFO
  
  If SHGetFileInfo_(@Path, #Null, @info, SizeOf(SHFILEINFO), #SHGFI_ICON|#SHGFI_SMALLICON)
    ProcedureReturn info\hIcon
  EndIf
  
EndProcedure
Procedure.l FreeIcon(hIcon.l)
  
  If hIcon
    DestroyIcon_(hIcon)
  EndIf
  
EndProcedure

;============================================

Structure FILESIZE
  High.l
  Low.l
EndStructure
Structure WIN32_FIND_DATA_2
  dwFileAttributes.l
  ftCreationTime.FILETIME
  ftLastAccessTime.FILETIME
  ftLastWriteTime.FILETIME
  nFileSizeLow.l
  nFileSizeHigh.l
  dwReserved0.l
  dwReserved1.l
  cFileName.s{260} ; replaced by fixed string
  cAlternate.s{14} ; replaced by fixed string
  dummy.w
EndStructure

Macro FtpOpen(Proxy = "", ProxyBypass = "")
  InternetOpen_("FTP", #INTERNET_OPEN_TYPE_DIRECT, Proxy, ProxyBypass, 0)
EndMacro
Macro FtpClose(hInternet)
  InternetCloseHandle_(hInternet)
EndMacro
Macro FtpConnect(hInternet, Host, User, Pwd, Port = 21)
  InternetConnect_(hInternet, Host, Port, User, Pwd, #INTERNET_SERVICE_FTP, 0, 0)
EndMacro
Macro FtpDisconnect(hConnect)
  InternetCloseHandle_(hConnect)
EndMacro

;============================================

Procedure.l FtpListAll(hConnect.l, folder.s = "/", depth.l = 0)
  
  Protected hIcon.l, hFind.l, Size.q, NewList find.WIN32_FIND_DATA_2()
  
  If FtpSetCurrentDirectory_(hConnect, folder) And AddElement(find())
    hFind = FtpFindFirstFile_(hConnect, 0, find(), 0, 0)
    If hFind 
      While AddElement(find()) And InternetFindNextFile_(hFind, find()) 
        ; Delay(1) 
      Wend
      InternetCloseHandle_(hFind)
      DeleteElement(find())
    EndIf
  EndIf
  
  ForEach find()
    If find()\dwFileAttributes & #FILE_ATTRIBUTE_DIRECTORY
      AddGadgetItem(0, -1, find()\cFileName, hIcon, depth)
      FtpListAll(hConnect, folder + find()\cFileName + "/", depth + 1)
    Else
      PokeL(@Size + 4, find()\nFileSizeLow)
      PokeL(@Size + 0, find()\nFileSizeHigh)
      AddGadgetItem(0, -1, find()\cFileName + " (" + StrByteSize(Size) + ")", hIcon, depth)
    EndIf
  Next
  
EndProcedure

;============================================

Define ftp.l, conn.l

If OpenWindow(0, 0, 0, 640, 480, "Ftp", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_ScreenCentered)
  
  If CreateGadgetList(WindowID(0))
    TreeGadget(0, 10, 10, 620, 460, #PB_Tree_AlwaysShowSelection)
  EndIf
  
  ftp = FtpOpen()
  If ftp
    ;conn = FtpConnect(ftp, "localhost", "user1", "*******")
    conn = FtpConnect(ftp, "localhost", "user2", "*******") ; <------------ changer cette ligne
    If conn
      FtpListAll(conn, "/")
      FtpDisconnect(conn)
    EndIf
    FtpClose(ftp)
  EndIf
  
  Repeat
    Select WaitWindowEvent() 
      Case #PB_Event_CloseWindow: Break
      Case #PB_Event_SizeWindow:  ResizeGadget(0, #PB_Ignore, #PB_Ignore, WindowWidth(0)-20, WindowHeight(0)-20)
    EndSelect
  ForEver
  
EndIf

;============================================ 

Publié : mar. 26/juin/2007 19:58
par lepiaf31
Merci super !
Juste une question, quelqu'un connait un moyen d'envoyer des fichiers sur le serveur parce que j'y arrive pas :oops:

Publié : mar. 26/juin/2007 20:00
par Flype
heurseusement que je donne le lien msdn...

va voir la fonction FtpPutFile()

Publié : mar. 26/juin/2007 20:04
par lepiaf31
c'est bon je sort ==>[] ^^
Merci a toi !

Publié : mar. 26/juin/2007 20:20
par Flype
pas'd'mal.

par contre si t'y arrive pas avec cette fonction, hésite pas...

c'est juste important de chercher un peu soi meme :D

Publié : mar. 26/juin/2007 20:22
par lepiaf31
Oui j'ai un petit probleme, je ne sais pas quoi mettre dans le dernier parametre: DWORD_PTR dwContext
(l'anglais et moi, c'est pas trop ca :p )
Merci

Publié : mar. 26/juin/2007 20:44
par Flype
en meme temps DWORD_PTR dwContext meme pour un anglais non-programmeur c'est du chinois :)


le dernier argument n'est pas obligatoire.
il permet de savoir où en est rendu la copie (avancement) pour les gros fichiers grâce à une fonction callback.

mais pour l'instant si on ignore cet argument (à coller dans l'ex ci dessus) :

Code : Tout sélectionner

    If conn
      ; FtpListAll(conn, "/")
      FtpSetCurrentDirectory_(conn, "/MARS/")
      FtpPutFile_(conn, "F:\JEUX\bintrudersetup.exe", "mon_transfert.exe", #FTP_TRANSFER_BINARY, #Null)
      FtpDisconnect(conn)
    EndIf

Publié : mar. 26/juin/2007 20:50
par lepiaf31
Super merci beaucoup (j'arrive à envoyer un fichier sur le serveur ! :D)

Publié : mar. 26/juin/2007 21:10
par Flype
bah voilà, et sans userlib, c'est toujours mieux. :wink: