Article View: borland.public.cppbuilder.internet.socket
Article #1784Re: Upload a file using PHP and http->Post ?
From: Nicholas Wilson
Date: Thu, 26 Oct 2006 01:29
Date: Thu, 26 Oct 2006 01:29
109 lines
3884 bytes
3884 bytes
Hi Remy,
Sorry for the delay, I only work here occasionally.
I am using the 9.15 version of the Indy component.
this is the sample code I cannot get to work.
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString
_html,
_form;
TMemoryStream
*_rc = new TMemoryStream (),
*_params = new TMemoryStream ();
TIdMultiPartFormDataStream
*_idmpfd = new TIdMultiPartFormDataStream ();
char
*_response;
ldhttp->Request->ContentType = _idmpfd->RequestContentType;
_idmpfd->AddFormField("userfile", "formsource.h");
ShowMessage (GetMIMETypeFromFile ("formsource.h"));
_idmpfd->AddFile("file","formsource.h", GetMIMETypeFromFile
("formsource.h"));
ShowMessage (_idmpfd->PrepareStreamForDispatch());
_idmpfd->Position = 0;
ldhttp->Post ("http://192.168.87.69/upload.php", _params, _rc);
ShowMessage ((char*)_rc->Memory);
}
this is the contents of the upload.php
//upload.php
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used
instead
// of $_FILES.
$uploaddir = './uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
// upload.html which works
<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="upload.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
Any help would be greatly appreciated.
Nicholas Wilson
"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message
news:4535cae8@newsgroups.borland.com...
>
> "Nicholas Wilson" <nicholasjohnwilson@gmail.com> wrote in message
> news:4535c1b4@newsgroups.borland.com...
>
> > there is much code example to post a file using the http->Post option
> > and the MultiPartFormDataStream. this seems to use either ASP or
> > isapi extensions. is there some way to do the same with PHP and apache?
>
> "multipart/form-data" uploads work the same in PHP as well. It is a
> standard HTTP mechanism.
>
> > I can upload using a web page, but I cant do the same with the indy
> component.
>
> Then you are likely not using it correctly. Please show your actual code.
> TIdMultiPartFormDataStream is specifically designed to mimic what a web
form
> submits to the server.
>
>
> Gambit
>
>
Technical Details
Message-ID:
<45400f1d@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news.glorb.com!newsfeed.cw.net!cw.net!news-FFM2.ecrc.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4535c1b4@newsgroups.borland.com> <4535cae8@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,3eee350dd19e6632 X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit Reply-To: "Nicholas Wilson" <nicholasjohnwilson@gmail.com> From: "Nicholas Wilson" <nicholasjohnwilson@gmail.com> Newsgroups: borland.public.cppbuilder.internet.socket References: <4535c1b4@newsgroups.borland.com> <4535cae8@newsgroups.borland.com> Subject: Re: Upload a file using PHP and http->Post ? Date: Thu, 26 Oct 2006 11:29:01 +1000 Lines: 111 Organization: None X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1807 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1807 NNTP-Posting-Host: 202.138.213.226 Message-ID: <45400f1d@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1161826077 202.138.213.226 (25 Oct 2006 18:27:57 -0700) Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news.glorb.com!newsfeed.cw.net!cw.net!news-FFM2.ecrc.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail