Delphi & PHP를 이용한 웹에 파일 올리기 입니다.
소스 내용에 대한 설명은 생략하겠습니다.
PHP와 Delphi indy를 사용하여 구현하였습니다.
SSL의 경우 무료SSL을 이용하여 IOHandler를 구성하면 되겠습니다.
소스 제공과 테스트를 해주신 반영재님 감사합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php $upload_dir = './data'; $upload_name = $_FILES["upfile"]["name"]; $maxfilesize = 102400000; if (is_uploaded_file($_FILES["upfile"]["tmp_name"])) { if($_FILES["upfile"]['size'] <= $maxfilesize) { if (move_uploaded_file($_FILES["upfile"]["tmp_name"], $upload_dir.'/'.$upload_name)) { chmod("$upload_dir/$upload_name", 0644); echo $upload_name. " upload finish"; } else { echo " upload fail"; } } else { echo " max file size over"; } } else { echo " file not selected"; } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, Vcl.ComCtrls; type TForm1 = class(TForm) Panel1: TPanel; Button1: TButton; IdHTTP1: TIdHTTP; Memo1: TMemo; OpenDialog1: TOpenDialog; ProgressBar1: TProgressBar; procedure Button1Click(Sender: TObject); procedure IdHTTP1Status(ASender: TObject; const AStatus: TIdStatus; const AStatusText: string); procedure IdHTTP1Work(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64); procedure IdHTTP1WorkBegin(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Int64); procedure IdHTTP1WorkEnd(ASender: TObject; AWorkMode: TWorkMode); private { Private } public { Public } end; var Form1: TForm1; implementation uses IdMultipartFormData, IdGlobal; {$R *.dfm} //============================================================================= // 파일 업로드 //============================================================================= procedure TForm1.Button1Click(Sender: TObject); var AUrl : String; DataPath : String; DataStream : TIdMultiPartFormDataStream; ResStream : TMemoryStream; begin if not OpenDialog1.Execute then exit; Memo1.Clear; //업로드 파일 DataPath := OpenDialog1.FileName; //업로드 한 파일을 처리하는 PHP의 절대 경로 AUrl := 'http://www.newrun.com/upload/upload_http.php'; DataStream := TIdMultiPartFormDataStream.Create; ResStream := TMemoryStream.Create; try //업로드 할 파일 지정 DataStream.AddFile('upfile', DataPath, ''); ProgressBar1.Max := DataStream.Size; ProgressBar1.Position := 0; //업로드 IdHTTP1.Post(AUrl, DataStream, ResStream); ResStream.Position := 0; //서버에서받은 문자열을 표시 Memo1.Lines.Add(''); Memo1.Lines.Add(IdGlobal.ReadStringFromStream(ResStream, -1)); finally FreeAndNil(DataStream); FreeAndNil(ResStream); end; end; //============================================================================= // IdHTTP1의 OnStatus 이벤트 // 연결 상태 등의 문자열을 Memo1에 표시 //============================================================================= procedure TForm1.IdHTTP1Status(ASender: TObject; const AStatus: TIdStatus; const AStatusText: string); begin Memo1.Lines.Append(AStatusText); end; procedure TForm1.IdHTTP1Work(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64); begin case AWorkMode of wmRead: ; wmWrite: ProgressBar1.Position := AWorkCount; end; end; procedure TForm1.IdHTTP1WorkBegin(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Int64); begin case AWorkMode of wmRead: ; wmWrite: Memo1.Lines.Add('WorkBegin = ' + AWorkCountMax.ToString); end; end; procedure TForm1.IdHTTP1WorkEnd(ASender: TObject; AWorkMode: TWorkMode); begin case AWorkMode of wmRead: ; wmWrite: Memo1.Lines.Add('WorkEnd'); end; end; end. |
최신 댓글