Wednesday 25 April 2012

FtpWebRequest: working with files outside of home folder

Something I ran recently against is the need to upload a file to FTP server using the FtpWebRequest class but to a location that is outside of my ftp user home folder. One would think this is no problem, but I found different.

The trick is in the URI being used. If you use ftp://server/folder1/folder2/filename.ext , what the FTP client does it issues a PWD command to the serer to find out the current folder. In this case the server will respond with your home folder like "/home/username/". Then the client will append this home folder to the folder you requested and will do CWD to "/home/username/folder1/folder2/" which in my case does not exist as my folder on the server is simply /folder1/folder2/.

So what to do now? I tried my luck and was amased with how much luck I have as it worked from the first attemtp - it is as simple as this: just specify the path in the follwing way ftp://server//folder1/folder2/filename.ext - see the two // after the server name ? This tells the client to ignore the user home folder and just do a CDW to the "/folder1/folder2/"

I hope this will help and save some time to some of you.