Board index » cppbuilder » Upload a file using PHP and http->Post ?
Nicholas Wilson
![]() CBuilder Developer |
Nicholas Wilson
![]() CBuilder Developer |
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. |
Remy Lebeau (TeamB)
![]() CBuilder Developer |
2006-10-18 02:21:24 PM
Re:Upload a file using PHP and http->Post ?
"Nicholas Wilson" < XXXX@XXXXX.COM >wrote in message
Quotethere is much code example to post a file using the http->Post option QuoteI can upload using a web page, but I cant do the same with the indy TIdMultiPartFormDataStream is specifically designed to mimic what a web form submits to the server. Gambit |
Nicholas Wilson
![]() CBuilder Developer |
2006-10-26 09:29:01 AM
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
{smallsort} |
Remy Lebeau (TeamB)
![]() CBuilder Developer |
2006-10-26 12:57:16 PM
Re:Upload a file using PHP and http->Post ?
"Nicholas Wilson" < XXXX@XXXXX.COM >wrote in message
QuoteI am using the 9.15 version of the Indy component. Quotethis is the sample code I cannot get to work. 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 |
Nicholas Wilson
![]() CBuilder Developer |
2006-10-26 01:12:52 PM
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
|
Remy Lebeau (TeamB)
![]() CBuilder Developer |
2006-10-26 03:30:15 PM
Re:Upload a file using PHP and http->Post ?
"Nicholas Wilson" < XXXX@XXXXX.COM >wrote in message
QuoteI could not see where to download that version from ? Gambit |