May 092016
 

Upload FIle with delphi and php

Assalamu’ alaikum wr wb

Upload File

Sekarang kita akan membahas sedikit aplikasi kecil untuk proses upload file dengan menggunakan delphi 7 sebagai client side nya dan php sebagai server side nya.

langsung saja buat form seperti pada gambar di atas.

lalu tulis stricpt di bawah ini :


unit UploadFile;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdMultipartFormData, IdHTTP, HTTPApp, ExtCtrls,
OleCtrls, SHDocVw;

type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
Panel1: TPanel;
Button2: TButton;
Button1: TButton;
Edit1: TEdit;
WebBrowser1: TWebBrowser;
ComboBox1: TComboBox;
Edit2: TEdit;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
Edit1.Text := OpenDialog1.FileName;
//OpenDialog1.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Stream: TStringStream;
Params: TIdMultipartFormDataStream;
HTTP: TIdHTTP;
url : String;
begin
{
Gambar
Excel
Word
Pdf
Other
}
Case ComboBox1.ItemIndex Of
0 : Begin
url := 'images';
end;
1 : begin
url := 'xls';
end;
2 : begin
url := 'doc';
end;
3 : begin
url := 'pdf';
end;
4 : begin
url := 'other';
end;
end;
Stream := TStringStream.Create('');
try
Params := TIdMultipartFormDataStream.Create;
try
Params.AddFile('fileToUpload', Edit1.Text ,'application/octet-stream');
try
HTTP := TIdHTTP.Create(Self);
HTTP.Post(Edit2.Text + '/upload_' + url + '.php', Params, Stream);
WebBrowser1.Navigate(Edit2.Text + '/upload-' + url);
//WebBrowser1.Refresh;
except
on E: Exception do
ShowMessage('Error encountered during POST: ' + E.Message);
end;
ShowMessage(Stream.DataString);
finally
Params.Free;
end;
finally
Stream.Free;
HTTP.Destroy;
end;
end;

end.

Lalu buat program di sisi server untuk menerima file yang di upload oleh delphi.

berikut source codenya :


<?php
$target_dir = "upload-other/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if(file_exists($target_file)) {
unlink( $target_file);
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>

simpan dengan nama : upload.php

Wassalamu’alaikum wr wb