help files
Z505 | PasWiki | FUQ | Search | Main Docs | API Guide



[Overview][Constants][Procedures and functions] Reference for unit 'pwumain' (#powtils_main)

SaveUpFile

Declaration

Source position: pwumain.pas line 187

function SaveUpFile(

  const name: String;

  const fname: String

):Boolean;

Notes

Saves uploaded file from clients computer to the server disk. Use this in combination with a 'browse for file' html form widget.

The name parameter is the name of the FORM 'browse for file' html widget that is passed in to the program. The fname parameter is a path of your choice to save the file on the server.

Tips

When saving files outside of cgi-bin, you can use SERV.docroot() from the pwenvvar unit (previously known as cgienvvar.docroot) to get the location to your document root (public html) directory.

Example

Note: you could use templates or rapidhtml to keep HTML design separate from logic, this is just a quickly thrown together example.
const FUPL = 'fupl';

procedure OutBr(s: string); 
begin 
  Out(s); Out('<br>'); 
end;

procedure ShowForm;
begin
  Out('<FORM METHOD=POST ACTION="" enctype="multipart/form-data">' +
         '<input name='+FUPL+' type=file size=20> ' +
         '<br><br>'+
         '<input type=submit>' +
       '</FORM>');
  Out('<p>Click to upload file and get info</p> ');
end;

procedure ProcessUpfile;
var FileOut: string;
    AbsoluteFname: string;
begin
  // note: make sure public_html/test/ exists on server
  FileOut:= '/test/'+GetUpFileName(FUPL);
  // absolute path to save file
  AbsoluteFname:= SERV.DocRoot() + FileOut;  
  OutBr('Upfile name: ' + GetUpFileName(FUPL)); 
  OutBr('Upfile type: ' + GetUpFileType(FUPL)); 
  OutBr('Upfile size: ' + IntToStr(GetUpFileSize(FUPL))); 
  if SaveUpFile(FUPL, absoluteFname) then
    Out('saved to: <a href="'+fileout+'">'+ fileout +'</a>' +
        '<br>absolute path: ' + absoluteFname);
  else
    Out('error saving file. example: directory may not exist' +
        '<br>tried to save to: ' + absoluteFname);
end;

begin
  // checks if user submitted file from form
  if IsUpFile(FUPL) then ProcessUpFile else ShowForm;
end.

See also






lufdoc, Powtils, fpc, freepascal, delphi, kylix, c/c++, mysql, cgi web framework docs, Z505