Jump to content

Need help with uploading file on FTP server


Recommended Posts

Ok.I read about FTP.au3 and I decided to use it so I can upload one text file on my FTP server.Some people said that it works and I also thin so but the problem is that isn't very well explained.Well, here's now my code:

#include <FTP.au3>
$server = "ftp.byethost13.com"
$username = "b13_1427267"
$pass = "this is my password"
$Open = _FTPOpen("ftp.byethost13.com")[color="#008000"][b];I'm not sure what to write here[/b][/color]
$Conn = _FTPConnect($Open, $server, $username, $pass)
$Ftpp = _FtpPutFile($Conn, "blabla.txt", "/htdocs/upload/blabla.txt")
$Ftpc = _FTPClose($Open)

The file I wanna upload is blabla.txt and I wanna upload it in /htdocs/upload folder.I changed the permissions of this folder so I can upload this file.Could you help me, please! :)

Link to comment
Share on other sites

Ok.I read about FTP.au3 and I decided to use it so I can upload one text file on my FTP server.Some people said that it works and I also thin so but the problem is that isn't very well explained.Well, here's now my code:

#include <FTP.au3>
$server = "ftp.byethost13.com"
$username = "b13_1427267"
$pass = "this is my password"
$Open = _FTPOpen("ftp.byethost13.com")[color="#008000"][b];I'm not sure what to write here[/b][/color]
$Conn = _FTPConnect($Open, $server, $username, $pass)
$Ftpp = _FtpPutFile($Conn, "blabla.txt", "/htdocs/upload/blabla.txt")
$Ftpc = _FTPClose($Open)

The file I wanna upload is blabla.txt and I wanna upload it in /htdocs/upload folder.I changed the permissions of this folder so I can upload this file.Could you help me, please! :)

You have to open the dll wininet.dll before you can use any FTP functions.... I don't know why but it doesn't work without it.

#include <FTP.au3>
$__ftp_hWinInetDll = DllOpen( 'wininet.dll' )
If $__ftp_hWinInetDll == -1 Then
    ConsoleWrite( "-- ERROR FROM FTP.au3: Unable to open WinInet.dll" & @LF )
    $__ftp_hWinInetDll = 'wininet.dll'
EndIf

$server = "ftp.byethost13.com"
$username = "b13_1427267"
$pass = "this is my password"
$Open = _FTPOpen("ftp.byethost13.com")
$Conn = _FTPConnect($Open, $server, $username, $pass)
$Ftpp = _FtpPutFile($Conn, "blabla.txt", "/htdocs/upload/blabla.txt")
$Ftpc = _FTPClose($Open)

Edit: also don't forget to put some error checking after _FTPOpen and _FTPConnect

Edited by rbhkamal

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

_FTPOpen is an initialization function just like tcpstartup() and udpstartup(), in that function you can set some flags check MSDN you just use the defaults.

_FTPConnect is the function that establish the actual connection to the FTP server.

Follow these steps:

1- Disable windows firewall

2- make sure that your FTP.au3 looks like the one I attached

3- Run the script below after you fill in the blanks:

#include <FTP.au3>

$server = "BLANK"
$username = "BLANK"
$pass = "BLANK"

$Open = _FTPOpen("anything")
consolewrite( "Open: " & $Open & @crlf )
$Conn = _FTPConnect($Open, $server, $username, $pass)
consolewrite( "Connect: " & $Conn & @crlf )
$Ftpp = _FtpPutFile($Conn, @ScriptDir&"\test.txt", "/home/test.txt")
consolewrite( "Put Result: " & $Ftpp & @crlf )
$Ftpc = _FTPClose($Open)

FTP.au3

Edit: I verified that the script below works just fine

Edited by rbhkamal

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

can you help me please?

What is the return value of _FTPFilePut()?

This sounds like the files on the local file on the machine wasn't found or the DATA connection failed to establish (probably a firewall blocking it), so the FTP server closed the file without writing anything to it. thus the empty file.

I'm sure the problem is not from FTP.au3 anymore, instead it just a flag or something similar. Make sure that the file you are trying to transfer exists on the local machine before you transfer it.

Then you should find out if the server uses passive or active ftp data connections and make sure that your firewall does not block it.

Here, use this script:

#include <FTP.au3>

$server = ""
$username = ""
$pass = ""
$sLocalFile = @ScriptDir&"\test.txt"
$sRemoteFile = "/home/test.txt"

;Local Const $INTERNET_FLAG_PASSIVE = 0x08000000
;Local Const $INTERNET_FLAG_TRANSFER_ASCII = 0x00000001
;Local Const $INTERNET_FLAG_TRANSFER_BINARY = 0x00000002
Local Const $WININET_API_FLAG_SYNC = 0x00000004



If Not FileExists( $sLocalFile ) Then
    MsgBox( 16 , "ERROR" , "File doesn't exist!" )
    Exit
EndIf


$Open = _FTPOpen("anything")
If @error Or Not $Open Then
    ConsoleWrite( "Error(" & @error & ") > Unable to initialize FTP.au3" & @LF )
    Exit
EndIf

$Conn = _FTPConnect($Open, $server, $username, $pass  );$INTERNET_FLAG_PASSIVE
If @error Or Not $Conn Then
    ConsoleWrite( "Error(" & @error & ") > Unable to establish the connection to "&$server & @LF )
    Exit
EndIf


$Ftpp = _FtpPutFile($Conn, $sLocalFile , $sRemoteFile );$WININET_API_FLAG_SYNC
If $Ftpp == 1 Then
    ConsoleWrite( "Transfer successful!" & @LF )
    Exit
EndIf
$Ftpc = _FTPClose($Open)

Edit: Try the $INTERNET_FLAG_PASSIVE flag after you're sure that the file exists on the local machine

Regards,

RK

Edited by rbhkamal

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

VERY BIG NOTE

It looks like the _FTPFilePut() transfers the file in the background so you have to wait.... give it time to finish uploading. OR just set the ASYNC flag

Global Const $WININET_API_FLAG_ASYNC     =  0x00000001  // force async operationoÝ÷ Øò¢ìÛh¶¶§±÷«ém^~)^²)íæÚrH+¢éݺǫ¶¬Êw!®èºÇåj®¶­sdvÆö&Â6öç7Bb33cµtääUEôôdÄuõ5ä2ÒBòòf÷&6R7æ2÷W&
Edited by rbhkamal

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...