Board index » cppbuilder » Upload a file using PHP and http->Post ?

Upload a file using PHP and http->Post ?


2006-10-18 01:54:26 PM
cppbuilder90
Hi,
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? I can
upload using a web page, but I cant do the same with the indy component.
thanks.
 
 

Re:Upload a file using PHP and http->Post ?

"Nicholas Wilson" < XXXX@XXXXX.COM >wrote in message
Quote
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.
Quote
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
 

Re:Upload a file using PHP and http->Post ?

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 ("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)" < XXXX@XXXXX.COM >wrote in message
Quote

"Nicholas Wilson" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.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


 

{smallsort}

Re:Upload a file using PHP and http->Post ?

"Nicholas Wilson" < XXXX@XXXXX.COM >wrote in message
Quote
I am using the 9.15 version of the Indy component.
That is a very old version. The current snapshot of Indy 9 is 9.0.50. You
should consider upgrading.
Quote
this is the sample code I cannot get to work.
It does not work because you are uploading the wrong stream to the server.
You are allocating a TMemoryStream named _params that you do not fill it
with any data but you ar uploading it anyway. You are then allocating a
TIdMultiPartFormDataStream named _idmpfd that you are not uploading when you
should be. You need to get rid of the _params stream altogether and upload
the _idmpfd stream instead.
Not only that, but you are filling in the TIdMultiPartFormDataStream
incorrectly anyway. You should not be adding the file name and file data as
separate fields. They belong to a single field in the HTML form, so you
have to add them to the stream as a single field.
Also, your code has memory leaks in it. You need to free the streams you
allocate.
Try this instead:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString _html;
TIdMultiPartFormDataStream *_idmpfd = new
TIdMultiPartFormDataStream;
try
{
ldhttp->Request->ContentType = _idmpfd->RequestContentType;
// AddFile() calls GetMIMETypeFromFile() when no ContentType is
specified
_idmpfd->AddFile("userfile", "formsource.h", "");
_html = ldhttp->Post ("192.168.87.69/upload.php",
_idmpfd);
}
__finally {
delete _idmpfd;
}
ShowMessage(_html);
}
Gambit
 

Re:Upload a file using PHP and http->Post ?

Thanks for that, may have been a case of staring for so long I could not see
the obvious. I
have been cutting and pasting code from all over the place so I have
obviously made a mistake.
I could not see where to download that version from ?
I am getting 9.0.15 from the indy site.
I will make the changes that you have pointed out see how I go. the memory
leak was intentional ( this is just testing at the moment);
cheers
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Nicholas Wilson" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>I am using the 9.15 version of the Indy component.

That is a very old version. The current snapshot of Indy 9 is 9.0.50.
You
should consider upgrading.

>this is the sample code I cannot get to work.

It does not work because you are uploading the wrong stream to the server.
You are allocating a TMemoryStream named _params that you do not fill it
with any data but you ar uploading it anyway. You are then allocating a
TIdMultiPartFormDataStream named _idmpfd that you are not uploading when
you
should be. You need to get rid of the _params stream altogether and
upload
the _idmpfd stream instead.

Not only that, but you are filling in the TIdMultiPartFormDataStream
incorrectly anyway. You should not be adding the file name and file data
as
separate fields. They belong to a single field in the HTML form, so you
have to add them to the stream as a single field.

Also, your code has memory leaks in it. You need to free the streams you
allocate.

Try this instead:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString _html;
TIdMultiPartFormDataStream *_idmpfd = new
TIdMultiPartFormDataStream;
try
{
ldhttp->Request->ContentType = _idmpfd->RequestContentType;

// AddFile() calls GetMIMETypeFromFile() when no ContentType
is
specified
_idmpfd->AddFile("userfile", "formsource.h", "");

_html = ldhttp->Post ("192.168.87.69/upload.php",
_idmpfd);
}
__finally {
delete _idmpfd;
}
ShowMessage(_html);
}


Gambit


 

Re:Upload a file using PHP and http->Post ?

"Nicholas Wilson" < XXXX@XXXXX.COM >wrote in message
Quote
I could not see where to download that version from ?
As I said, 9.0.50 is the current development snapshot. The link is on the
Indy website.
Gambit