Recent

Author Topic: Putting a new file on a web-site  (Read 932 times)

J-G

  • Hero Member
  • *****
  • Posts: 1216
Putting a new file on a web-site
« on: July 16, 2026, 02:43:35 pm »
A few years ago I was aided admirably in my endevours to handle files on my web-site  by @Molly  who was last seen hereabouts in 2021.

I've looked back at code that @Molly provided in 2017 and have reached a half-way-house.  That is I can READ the information that exists in a TEXT file on my site but cannot over-write that file with new (updated) data.

I'm using fpHttpClient thus :

To read the existing data :
Code: Pascal  [Select][+][-]
  1. procedure Get_Web_Version;
  2. var
  3.   S : string;
  4. begin
  5.   with TFPHttpClient.Create(Nil) do
  6.    try
  7.      S := Get('http:// {My URL} /Version.txt');
  8.    finally
  9.      Free;
  10.    end;
  11.   Delete(S,1,6);  // this just removes the pre-amble 'Ver = ' from the text
  12.   WebVer := S;
  13. end;
  14.  

and this is the (non-working) replace code :
 
Code: Pascal  [Select][+][-]
  1. procedure Write_Version;
  2. begin
  3.   Assign(V_File,DataPath+'Version.txt');
  4.   Rewrite(V_File);
  5.   write(V_File,'Ver = '+Version);
  6.   close(V_File);
  7. end;
  8.  
  9. procedure Put_Web_Version;
  10. begin
  11.   Write_Version;
  12.   with TFPHttpClient.Create(Nil) do
  13.    try
  14.      Put('http:// {my URL}','Version.txt');
  15.    finally
  16.      Free;
  17.    end;
  18. end;
  19.  

This compiles without error but stepping through and checking what is actually on the web (via FileZilla) - I see that the timestamp on the web is not altered (after the 'Put') and naturally copying the file back to my local PC (different folder!) shows that the new file which has been created in the Write_Version proc. has not been 'put' on the web.

There is no 'error' returned by the Put. . . .

I'm obviously missing something but can't fathom what.

You'll appreciate that this is just the start of a process that may well cause me to ask many more questions  :o
« Last Edit: July 16, 2026, 02:46:05 pm by J-G »
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 1216
Re: Putting a new file on a web-site
« Reply #1 on: July 16, 2026, 06:24:49 pm »
After a little further research I see from my Web-site logs that the [Put] appears to attract a 405 error which, looking through the docs for .htAccess,  is 'Method not allowed'.

Although I know that to provide access to my web-site by whomever, I need to have an htAccess file in the particular folder. What I don't know is what should be in that file. I've used ths same file for many years and it containes just two lines :

Options All Indexes
IndexOptions FancyIndexing NameWidth=*


This has always allowed anyone I give the URL to, access to download any files held in that specific folder.

I see from further reading that there is a 'RewriteEngine' which seems to take the argument  'on' (I presume thta there may also be an 'off')  but adding that did not change the behaviour.

Can anyone shed light on whether the 'writing' is blocked simply because I haven't specifically allowed it (in htAccess) and if so what do I need to put in htAccess to make it happen?

I can appreciate that 'Reading' a file would not be resticted in this way; and that 'Writing' might be due to 'security' issues.

[EDIT] 
I've now tried  'Post' rather than 'Put'  and I've also added my IP address as 'Allow From' in the .htAccess file.

Still no change to the file held on the web.

I can of course use FileZilla to put the new file on the page but I'd sooner do it from within my program.  %)

« Last Edit: July 16, 2026, 07:46:26 pm by J-G »
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

paweld

  • Hero Member
  • *****
  • Posts: 1701
Re: Putting a new file on a web-site
« Reply #2 on: July 17, 2026, 07:29:40 am »
Uploading a file to a website in this way would require some kind of API that supports this functionality (I haven't come across a web server that supports this on its own).
But you can upload such a file programmatically using the FTP protocol, in which case you wouldn't need to use FileZilla.
Below is a simple example - the Synapse package must be installed in Lazarus, which is available in OPM
Code: Pascal  [Select][+][-]
  1. uses
  2.   ftpsend;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. begin
  6.   //             ftp hostname  | port | remote path + filename |  local filename | ftp login | ftp password
  7.   if FtpPutFile('ftp.domena.pl', '21', 'pubic_html/Version.txt', 'd:\Version.txt', 'webadmin', 'Secret123') then
  8.     ShowMessage('The file has been sent')
  9.   else
  10.     ShowMessage('An error occurred while sending the file');
  11. end;
Best regards / Pozdrawiam
paweld

LeP

  • Guest
Re: Putting a new file on a web-site
« Reply #3 on: July 17, 2026, 09:02:35 am »
Uploading a file to a website in this way would require some kind of API that supports this functionality (I haven't come across a web server that supports this on its own).
What's the problem with handling the HTTP protocol's "PUT" command?
On my web server, I get a streaming file (with all the necessary checks), and if it passes all the checks, I write it to disk (I use Indy).

MarkMLl

  • Hero Member
  • *****
  • Posts: 8579
Re: Putting a new file on a web-site
« Reply #4 on: July 17, 2026, 09:03:20 am »
After a little further research I see from my Web-site logs that the [Put] appears to attract a 405 error which, looking through the docs for .htAccess,  is 'Method not allowed'.

You need to ensure that your webserver (e.g. Apache) is specifically configured to support the Put method. It used to be (in v1), but that was dropped in favour- I believe- of things like WebDAV.

https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Method

It's a foulup fairly typical of the industry, which has inevitably moved from things which were "nice and simple" to things which... aren't. That's why so many people still use FTP for this sort of thing, and ignore the potential consequences of weak security etc.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

J-G

  • Hero Member
  • *****
  • Posts: 1216
Re: Putting a new file on a web-site
« Reply #5 on: July 17, 2026, 10:10:54 am »
Uploading a file to a website in this way would require some kind of API that supports this functionality (I haven't come across a web server that supports this on its own).
But you can upload such a file programmatically using the FTP protocol, in which case you wouldn't need to use FileZilla.
Below is a simple example - the Synapse package must be installed in Lazarus, which is available in OPM
Code: Pascal  [Select][+][-]
  1. uses
  2.   ftpsend;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. begin
  6.   //             ftp hostname  | port | remote path + filename |  local filename | ftp login | ftp password
  7.   if FtpPutFile('ftp.domena.pl', '21', 'pubic_html/Version.txt', 'd:\Version.txt', 'webadmin', 'Secret123') then
  8.     ShowMessage('The file has been sent')
  9.   else
  10.     ShowMessage('An error occurred while sending the file');
  11. end;
With no response after over 100 views and over 40 hours, I was begining to think that I might be asking for the impossible  :D

FTP protocol had been going through my mind and of course I can find the relevant passwords etc. since they have to be used in FileZilla.

I have (since reading your post @paweld - Thanks) found Synapse 40.1 through OPM and presume that there is no detrement to installing such  -  I'll report back when I've had a proper look.

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

paweld

  • Hero Member
  • *****
  • Posts: 1701
Re: Putting a new file on a web-site
« Reply #6 on: July 17, 2026, 11:50:31 am »
Quote from: LeP
What's the problem with handling the HTTP protocol's "PUT" command?
On my web server, I get a streaming file (with all the necessary checks), and if it passes all the checks, I write it to disk (I use Indy).
Thank you for the information. I suspect this requires some kind of special Apache/Nginx configuration, and I'm wondering if it's possible to set this up with standard web hosting, or if you simply have to dig deeper - in which case, would it require a fully managed server?
Best regards / Pozdrawiam
paweld

J-G

  • Hero Member
  • *****
  • Posts: 1216
Re: Putting a new file on a web-site
« Reply #7 on: July 17, 2026, 11:58:25 am »
???  -  Do I have a problem ??

I've selected Synapse v40.1 from the OPM List and clicked [Install]  -  OPM tells me that it IS installed - so I've done some adjustments to my code to make use of FTP.

The strapline of the IDE now reads 'Lazarus IDE v2.2.0 - Triangle (running ...)' and [ f9 ] doesn't 'compile' ??  -  An attempt to 'Close' ( [ X ] ) brings up 'Stop debugging' but the program is not 'debugging' (I've previously used [ ctrl ][ f2 ])  and I respond 'Yes'.

Short of closing my PC down what else could get me out of this impass?
« Last Edit: July 17, 2026, 12:02:11 pm by J-G »
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 1216
Re: Putting a new file on a web-site
« Reply #8 on: July 17, 2026, 12:13:17 pm »
Panic over  -  I used [ctrl][f9] which asked 'Stop debugging & rebuild'

BUT  - now [f9] (and [Crtl][f9]) reports :
tri.pas(19,17) Fatal: Cannot find FTPSend used by Tri of the Project Inspector.

I'm obviously incompetent in some measure but cannot fathom where  :o

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

paweld

  • Hero Member
  • *****
  • Posts: 1701
Re: Putting a new file on a web-site
« Reply #9 on: July 17, 2026, 12:33:36 pm »
You need to add the “laz_synapse” package to the project requirements:
Code: [Select]
Project Menu > Project Inspector > Add > New requirement > search for “laz_synapse” > click “OK”
Best regards / Pozdrawiam
paweld

J-G

  • Hero Member
  • *****
  • Posts: 1216
Re: Putting a new file on a web-site
« Reply #10 on: July 17, 2026, 01:04:26 pm »
Ah!  - It is a very long time since I 'added' a component to Lazarus ! 

I have now successfully re-compiled and stepped through the new 'Put_Web_Version' Proc which is :

Code: Pascal  [Select][+][-]
  1. procedure Put_Web_Version;
  2. begin
  3.   Write_Version;
  4.  
  5.   FTPRec.FN     := 'Version.txt';
  6.   FTPRec.R_Path := 'crescentcomputing.co.uk/Triangle/Version.txt';
  7.  
  8.   If FtpPutFile(FTPRec.Host,
  9.          FTPRec.Port,
  10.          FTPRec.R_Path,
  11.          FTPRec.FN,
  12.          FTPRec.Login,
  13.          FTPRec.P_Word) then
  14.     OK := True
  15.   else
  16.     begin
  17.       Alert;
  18.       OK := False;
  19.       TriangleSolution.MessageBoard.Caption := 'Writing Version File to WEB Failed';
  20.     end;
  21. end;
  22.  

You'll notice that I've created a Record of the FTP settings (FTPRec) - I was a little surprised that the 'Port' was not allowed to be a 'byte', after all it is a small number but String[2] was allowed.

The 'FtpPutFile' took an appreciable time and on first test 'failed' so I now need to look again at the Web Logs to see which argument(s) are incorrect.

Time for breakfast & medication  :D  - - -  I'll be back!


FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 1216
Re: Putting a new file on a web-site
« Reply #11 on: July 17, 2026, 11:29:54 pm »
Regretably I'm no further forward - having spent quite some time writing a program to analise the sftp.log and access.log files which appear to record all activity on the web-site.

There are entries which appear to confirm that the Password I provide (as gleaned from FileZilla) is acceptable - witness the entry "Accepted password for u114218502" as are  the 'Login' cridentials.

I'm a little unsure about the 'Port';  in FileZilla that is an empty field so I've tested using 'blank', '20', '21' & '22' all have the same effect  -  I suspect that 'port' is ignored.

The only other variable that I can think may have an effect is the Remote Path. I've tried with and without 'http://' at the start.

All variations return from FtpPutFile with a failure.  It seems that I'm close but 'no cigar'  :(

Can anyone offer another option to try?

[EDIT] 
After further tests  - using a known to be incorrect password - I still see an entry in the .log file that says "Accepted password for u114218502"   so it seems that is not reporting that the password is 'correct', only that it has been accepted.   %)

I also notice that the 'Port' is always reported to be '22' - irrespective of whatever I've set it at for the call, which appears to confirm that 'port' is ignored.
« Last Edit: July 17, 2026, 11:56:03 pm by J-G »
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

lainz

  • Hero Member
  • *****
  • Posts: 4744
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Putting a new file on a web-site
« Reply #12 on: July 17, 2026, 11:51:04 pm »
You're using FTP or FTPS?

J-G

  • Hero Member
  • *****
  • Posts: 1216
Re: Putting a new file on a web-site
« Reply #13 on: July 18, 2026, 02:08:49 am »
You're using FTP or FTPS?

! @lainz  -  long time no 'chat'  -  Great to know that you are still around !

To answer your question - I'm not sure.  How could that be?  Well,  I'm simply using what has been suggested.  I've installed Synapse and added FTPSend to the Uses. Beyond that, I'm making a call with these arguments (which I think are correct)

Code: Pascal  [Select][+][-]
  1. FtpPutFile(FTPRec.Host,FTPRec.Port,FTPRec.R_Path,FTPRec.FN,FTPRec.Login,FTPRec.P_Word)

I do think that the 'Host', 'R_Path','FN' & 'Login'  are correct and I believe that P_Word is also correct though I will have to check that with the Web Hosting Admins tomorrow (It's just past 1 am here and I've been 'at it'' since about 8am) - I say 'I think' because I'm not 100% sure if I have 'Login' correct because I've taken the details from my FileZilla profile but they use 'User' rather than 'Login' - though I have no other options that I can determine.

I do know that in FileZilla I have 'Protocol' set to SFTP - SSH rather than just FTP and that has been working for a very long time.

Also, I've just checked the ftpsend Unit and there is no mention of 'FTPS'.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

jamie

  • Hero Member
  • *****
  • Posts: 7902
Re: Putting a new file on a web-site
« Reply #14 on: July 18, 2026, 02:26:32 am »
you could load Indy, it has all of that stuff.

FTP, WebDav etc.

all with Secure socket options where they apply.


Jamie


The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018