Hi,
I've a local server with LAMP and have a plan to upload any file(s) with Synapse + Lazarus to the LAMP server.
For a few days I still confuse with Synapse library because lack of sample(s) and lack of my brain of course
.
This is my simple PHP scripts:
<?php
//process.php
$upload_dir = "doc/";
foreach ($_FILES["docfile"]['name'] as $f => $name) {
if((!empty($_FILES["docfile"]['name'][$f])))
{
$filename = basename($_FILES['docfile']['name'][$f]);
$ext = substr($filename, strrpos($filename, '.') + 1);
$newname = $upload_dir.$filename;
if (!file_exists($newname))
{
if ((move_uploaded_file($_FILES['docfile']['tmp_name'][$f],$newname)))
{
//Whatever :)
}
} else {
//Whatever :)
}
}
}
?>
<?php
//form.php
?>
<form enctype="multipart/form-data" action="fileserver/process.php" method="post">
<input required name="docfile[]" type="file" multiple="multiple">
<input type="submit" value="Send now">
</form>
Finally the Lazarus/Pascal codes which I found it from Google search result:
uses
..., synautil, synacode;
function HttpPostFiles(const URL: string; const Files: array of string;
ResultData: TStrings): boolean;
var
HTTP: THTTPSend;
Bound: string;
i: integer;
Filename: string;
begin
Bound := IntToHex(Random(MaxInt), 8) + '_Synapse_boundary';
HTTP := THTTPSend.Create;
try
for i := Low(Files) to High(Files) do
if FileExists(Files[i]) then
begin
Filename := ExtractFileName(Files[i]);
WriteStrToStream(HTTP.Document, CRLF + '--' + Bound + CRLF +
'Content-Disposition: form-data; name=' + AnsiQuotedStr(Filename, '"') +
';' + CRLF + #9'filename=' + AnsiQuotedStr(Filename, '"') +
CRLF + 'Content-Type: application/octet-string' + CRLF + CRLF);
HTTP.Document.LoadFromFile(Files[i]);
end;
WriteStrToStream(HTTP.Document, CRLF + '--' + Bound + '--' + CRLF);
HTTP.MimeType := 'multipart/form-data; boundary=' + Bound;
Result := HTTP.HTTPMethod('POST', URL);
if Result then
ResultData.LoadFromStream(HTTP.Document);
finally
HTTP.Free;
end;
end;
procedure Tloginform.Button1Click(Sender: TObject);
var
ResultData: TStringList;
begin
ResultData := TStringList.Create;
try
if HttpPostFiles('http://192.168.43.176/fileserver/process.php',
['E:\development\software\lazarus\47-fileserver\test.txt', 'E:\development\software\lazarus\47-fileserver\tust.txt'], ResultData) then
ShowMessage(ResultData.Text);
finally
ResultData.Free;
end;
end;
But, I can not produce any result with Lazarus/Pascal on my local webhost server. Please help. Many thanks..