Question

I have to upload a file with my delphi program and handle server side with php

This is my complete code :

unit Unit6;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,
  Vcl.ComCtrls, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack,IdSSLOpenSSL;

type
  TForm6 = class(TForm)
    IdHTTP1: TIdHTTP;
    BitBtn1: TBitBtn;
    od1: TOpenDialog;
    ProgressBar1: TProgressBar;
    m1: TMemo;
    IdIOHandlerStack1: TIdIOHandlerStack;
    procedure BitBtn1Click(Sender: TObject);
    procedure HTTPWorkBegin(Sender: TObject; AWorkMode: TWorkMode;const AWorkCountMax: Integer);
    procedure IdHTTPWork(Sender: TObject; AWorkMode: TWorkMode;const AWorkCount: Integer);
    procedure IdHTTPWorkEnd(Sender: TObject; AWorkMode: TWorkMode);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form6: TForm6;

implementation

{$R *.dfm}

procedure TForm6.HTTPWorkBegin(Sender: TObject; AWorkMode: TWorkMode;
  const AWorkCountMax: Integer);
begin
   if AWorkMode = wmRead then
   begin
      ProgressBar1.Max := AWorkCountMax;
      ProgressBar1.Position := 0;
   end;
end;

procedure TForm6.IdHTTPWork(Sender: TObject; AWorkMode: TWorkMode;
  const AWorkCount: Integer);
begin
   if AWorkMode=wmRead then
     ProgressBar1.Position := AWorkCount;
end;


procedure TForm6.IdHTTPWorkEnd(Sender: TObject; AWorkMode: TWorkMode);
begin
  ProgressBar1.Position := 0;
end;

procedure TForm6.BitBtn1Click(Sender: TObject);
var
 Response:string;
   LHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
  if od1.Execute then
  begin
 //   LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
   // IdHTTP1.IOHandler:=LHandler;
    Response := IdHTTP1.Post('http://localhost/delphi/index.php?asd', od1.FileName);
    m1.Text := Response;
  end;
end;

end.

The server side is in php:

test1
<?php
    print_r($_FILES);
?>

also i changed it to:

test1
<?php
    print_r($_POST);
?>

but at the all tests the delphi response is an empty array.

test1
Array
(
)

Which part of my code is the problem?!

Was it helpful?

Solution

You are passing a filename to TIdHTTP.Post(). That posts the raw content of the file as-is. That will not populate PHP's $_FILES array (used for multipart/form-data posts) or $_POST array (used for application/x-www-form-urlencoded posts).

Add the file to a TIdMultipartFormDataStream and post that instead. It will send a multipart/form-data post that should populate the $_FILES array, eg:

uses
  ..., IdMultipartFormData;

procedure TForm6.BitBtn1Click(Sender: TObject);
var
  //LHandler: TIdSSLIOHandlerSocketOpenSSL;
  PostData: TIdMultipartFormDataStream;
begin
  if od1.Execute then
  begin
    //LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
    //IdHTTP1.IOHandler := LHandler;

    PostData := TIdMultipartFormDataStream.Create;
    try
      PostData.AddFile('file', od1.FileName);
      m1.Text := IdHTTP1.Post('http://localhost/delphi/index.php?asd', PostData);
    finally
      PostData.Free;
    end;
  end;
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top