So I am making a website for my family, where we can upload our images and view them, but an important feature of the website is to sort by date so that when for example my aunt have taken pictures at my mothers birthday and I also have taken pictures and we upload the images they will be added to the same album etc.
I've realized that it is not possible to preserve the date, when uploading through a browser. So I will make a small program which is only used for upload pictures. I have an FTP server running but when ever I upload images the date will change to current datetime. I have found the answer to why it does that so now I am looking for a way to preserve the date while uploading to FTP.
Here's some ideas I've had:
- If the program adds the files to a zip file and upload that zip file they will preserve the date, but that means I would have to have something on the server that unpacks the zips.
- When the images gets uploaded the program extracts the created date from the original image and adds it to a text file which it also uploads, but that would again require a program on the server which changes the uploaded images created date.
- Maybe I upload the images and thereafter change the uploaded images created date from the client?
- Maybe I upload the images and thereafter change the uploaded images created date from the client?
In FTP protocol, use MFMT
or MDTM
command to update file modification timestamp, or MFCT
to update file creation timestamp, depending on which of these your FTP server supports.
Actually none of them is standardized.
- The
MFMT
and MFCT
are drafted here: https://tools.ietf.org/html/draft-somers-ftp-mfxx-04
- The
MDTM
is defined in RFC 3659 to retrieve file modification timestamp, using MDTM filename
syntax. But many FTP servers support an alternative (non-standard) syntax MDTM filename timestamp
(i.e. the same as proposed MFMT
) to update the modification timestamp too.
Though the native FTP implementation in .NET framework (the FtpWebRequest
or the WebClient
wrapper), does not support any of these.
You have to use a 3rd party library.
For example the WinSCP .NET assembly preserves the modification timestamp automatically for any upload (or download) without any additional code.
A simple example code to upload a file (implicitly preserving the modification timestamp):
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Upload
session.PutFiles(@"d:\toupload\image.jpg", "/home/user/").Check();
}
For details, see Session.PutFiles
.
The latest version of WinSCP GUI FTP client (5.9 and newer) can even generate the C# code for you.
(I'm the author of WinSCP)