Im trying to check if a directory exists on an FTP server. Before you say "use ListDirectory" or "use PrintWorkingDirectory", they dont always work; for example, I tested if ftp://webserver/Logs existed and both told me it does when it actually doesnt. So Ive gone down the route of uploading a file to the directory and if successful, then the directory exists.
The problem is, the below method doesnt work with GoDaddy's CentOS based server's running vsFTPd 2.0.7.2. Works fine with Microsoft FTP server on IIS7.5.
So I monitored the traffic with Wireshark and used Filezilla to see what it was doing that my App wasnt to make it work. And the only difference is Filezilla is changing the working directory where as I am trying to upload the file with a path before it.
I have a feeling its something to do with the path its uploading to the server and the interpretation by Linux, cause it can be a bit funny with names... :-D Any ideas warmly welcomed?
App code
private bool DirectoryExists(string d)
{
bool exists = true;
try
{
string file = "directoryexists.test";
string path = url + homepath + d + "/" + file;
//Try to save to the directory
req = (FtpWebRequest)WebRequest.Create(path);
req.ConnectionGroupName = "conngroup1";
req.Method = WebRequestMethods.Ftp.UploadFile;
if (nc != null) req.Credentials = nc;
if (cbSSL.Checked) req.EnableSsl = true;
req.Timeout = 10000;
byte[] fileContents = System.Text.Encoding.Unicode.GetBytes("SAFE TO DELETE");
req.ContentLength = fileContents.Length;
Stream s = req.GetRequestStream();
s.Write(fileContents, 0, fileContents.Length);
s.Close();
//Delete file if successful
req = (FtpWebRequest)WebRequest.Create(path);
req.ConnectionGroupName = "conngroup1";
req.Method = WebRequestMethods.Ftp.DeleteFile;
if (nc != null) req.Credentials = nc;
if (cbSSL.Checked) req.EnableSsl = true;
req.Timeout = 10000;
res = (FtpWebResponse)req.GetResponse();
res.Close();
}
catch (WebException ex)
{
exists = false;
}
return exists;
}
Filezilla log via Wireshark
Response: 230 Login successful.
Request: CWD /Home/test1
Response: 250 Directory successfully changed.
Request: TYPE I
Response: 200 Switching to Binary mode.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,71,209)
Request: LIST
Response: 150 Here comes the directory listing.
FTP Data: 78 bytes
Response: 226 Directory send OK.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,177,1)
Request: STOR directoryexists.txt
Response: 150 Ok to send data.
Response: 226 File receive OK.
App log via Wireshark
Response: 230 Login successful.
Request: OPTS utf8 on
Response: 501 Option not understood.
Request: PWD
Response: 257 "/Home/"
Request: PWD
Response: 257 "/Home/"
Request: TYPE I
Response: 200 Switching to Binary mode.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,217,87)
Request: STOR test1/directoryexists.txt
Response: 553 Could not create file.
It creates the folders if they dont exist.
Response: 230 Login successful.
Request: PWD
Response: 257 "/Home/"
Request: PWD
Response: 257 "/Home/"
Request: TYPE I
Response: 200 Switching to Binary mode.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,220,60)
Request: STOR Logs/directoryexists.txt
Response: 553 Could not create file.
Request: PWD
Response: 257 "/Home/"
Request: MKD Logs
Response: 257 Create folder operation successful.
Request: TYPE I
Response: 200 Switching to Binary mode.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,255,245)
Request: STOR Logs/LogFiles/directoryexists.txt
Response: 553 Could not create file.
Request: PWD
Response: 257 "/Home/"
Request: MKD Logs/LogFiles
Response: 257 Create folder operation successful.
STOR test1/directoryexists.txt
. Is it possible that you cannot create another nested directory under /Home/ on the FTP server? Some hosting providers limit on how deep you can nest directorie, so you may /not/be/allowed/to/put/a/folder/this/deep or even /this/deep. According to wireshark you are in /Home/ so you should simply just be able to executeSTOR directoryexists.txt
. – ShuttlecockResponse: 257 "/Home/A/B/C/"
– Neighreq.Method
toWebRequestMethods.Ftp.MakeDirectory
, creating the directory, and then trying to upload the file? – Shuttlecock