🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.internet.socket
6 messages
6 total messages Started by Nicholas Wilson Wed, 18 Oct 2006 05:54
Upload a file using PHP and http->Post ?
#1776
Author: Nicholas Wilson
Date: Wed, 18 Oct 2006 05:54
10 lines
1248 bytes
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 ?
#1777
Author: Remy Lebeau (TeamB)
Date: Wed, 18 Oct 2006 06:21
20 lines
1620 bytes
"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
Re: Upload a file using PHP and http->Post ?
#1784
Author: Nicholas Wilson
Date: Thu, 26 Oct 2006 01:29
109 lines
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
>
>
Re: Upload a file using PHP and http->Post ?
#1785
Author: Remy Lebeau (TeamB)
Date: Thu, 26 Oct 2006 04:57
52 lines
2747 bytes
"Nicholas Wilson" <nicholasjohnwilson@gmail.com> wrote in message
news:45400f1d@newsgroups.borland.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 ("http://192.168.87.69/upload.php",
_idmpfd);
        }
        __finally {
            delete _idmpfd;
        }
        ShowMessage(_html);
    }


Gambit
Re: Upload a file using PHP and http->Post ?
#1786
Author: Nicholas Wilson
Date: Thu, 26 Oct 2006 05:12
74 lines
3569 bytes
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)" <no.spam@no.spam.com> wrote in message
news:4540405a$1@newsgroups.borland.com...
>
> "Nicholas Wilson" <nicholasjohnwilson@gmail.com> wrote in message
> news:45400f1d@newsgroups.borland.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 ("http://192.168.87.69/upload.php",
> _idmpfd);
>         }
>         __finally {
>             delete _idmpfd;
>         }
>         ShowMessage(_html);
>     }
>
>
> Gambit
>
>
Re: Upload a file using PHP and http->Post ?
#1787
Author: Remy Lebeau (TeamB)
Date: Thu, 26 Oct 2006 07:30
11 lines
1330 bytes
"Nicholas Wilson" <nicholasjohnwilson@gmail.com> wrote in message
news:45404395@newsgroups.borland.com...

> 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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads