Solved

Upload file to web server

Posted on 2007-11-15
17
7,929 Views
Last Modified: 2008-02-01
Hi guys,

I have the following code in php (uploadfile.php):

<?
if(isset($_FILES['file']))
{
$arquivo = $_FILES['file'];
$ziparq_dir = 'Myfiles/' . $arquivo['name'];
if(move_uploaded_file($arquivo['tmp_name'], $ziparq_dir))
echo "ok";
else
echo "uploaded file was not moved";
}
else
echo "uploaded file is not available";
?>

I am trying to do a program in delphi to upload zip files to my webserver, but some think is wrong because this code dont work.
.
My code in delphi is:

  var
    Strm: TIdMultipartFormDataStream;
  begin
    Strm := TIdMultipartFormDataStream.Create;
        try
          Strm.AddFile('file', 'c:\test.zip',GetMimeTypeFromFile('c:\test.zip'));
           IdHTTP1.Post('http://www.mysite.com.br/uploadfile.php', Strm);
        finally
            Strm.Free;
        end;
  end;

Please help me

Thx
Alexandre
Brazil
0
Comment
Question by:alpires
  • 6
  • 5
  • 3
  • +1
17 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 20288753
Hi,
    Strm.Position := 0;
Just before Post call
1
 

Author Comment

by:alpires
ID: 20289118
I dont know what, but dont worked. My sniffer show me with the file is not being sent to the server

Some idea ? Or another way to do this without indy ?
0
 
LVL 17

Expert Comment

by:mokule
ID: 20289378
This is what I've got in a working program

      UploadFile := TIdMultiPartFormDataStream.Create;
      UploadFile.AddFile('xml', AFileName, 'text');
      UploadFile.Position := 0;

      with TIdHTTP.Create(self) do
        begin
        try
          Memo1.Lines.Text := Post(cbClientIP.Text, UploadFile);
        finally
          Free;
          end;
        end;

      UploadFile.Free;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:alpires
ID: 20290932
Something is wrong here because this does not work here...very strange, my sniffer continues showing that no file was posted. I dont know what happens
0
 
LVL 17

Expert Comment

by:mokule
ID: 20291425
You've got
          IdHTTP1.Post('http://www.mysite.com.br/uploadfile.php', Strm);
I'm specifing only server address without file name as the first parameter of Post
0
 

Author Comment

by:alpires
ID: 20291543
My server dont accept upload files directly
0
 
LVL 2

Accepted Solution

by:
bogdan_iuly earned 500 total points
ID: 20337631
Hello,

You can use TWebBrowser to upload the file, just like in this example:
http://delphi.about.com/od/delphitips2007/qt/httppost_delphi.htm

If you need more info, I'll be glad to help you further.
0
 
LVL 9

Expert Comment

by:alex_code
ID: 20372023
Hi bogdan_iuly,alpires

Did you test this solution?it workS?Can you help me to use this procedure because i tried to use it but i get some errors.PLease if you can help me post an example how to use it.

Thanks in advance!
0
 
LVL 9

Expert Comment

by:alex_code
ID: 20372103
i drop on my form a twebrowser, i use the name to the procedure like this

UploadFilesHttpPost(WebBrowser1,'http://www.mysite.gr/myimages/','apicture.jpg');

...but i get this error:

constants cannot be used as open array arguments.

Which is the right syntax to call this procedure?
0
 
LVL 2

Expert Comment

by:bogdan_iuly
ID: 20372165
Hello alex_code,

Remember how the procedure is defined:
procedure UploadFilesHttpPost(
  const wb:TWebBrowser;
  const URLstring: string;
  names, values, nFiles, vFiles: array of string) ;

You have arrays for uploading multiple files, so you can't use 'apicture.jpg', you must use ['apicture.jpg'].

You must also specify to which variable name the file will be uploaded.
<input type="file" id="uploaded_file" name="uploaded_file" size="30" />

You should use the function as follows:
UploadFilesHttpPost(
  WebBrowser1,
  'http://www.domain.com/upload.php',
  [],  [],  ['uploaded_file'],  ['apicture.jpg'] );
0
 
LVL 9

Expert Comment

by:alex_code
ID: 20372341
hi bogdan_iuly and thank you for the post.

So here is my first mistake.This procedure use an existing upload file for example a php upload file and you post the file with this procedure.

I 'll try later the example:
~~~~~~~~~~~~~~~~~~~~~~~
UploadFilesHttpPost(
  WebBrowser1,
  'http://www.domain.com/upload.php',
  [],  [],  ['uploaded_file'],  ['apicture.jpg'] );
~~~~~~~~~~~~~~~~~~~~~~~

and i 'll post back for the results but i want to ask one more thing.Is there any way just to copy a local picture or a file to a remote server for example to copy an image from my local pc to myh website to a path like www.mysite.com/images/.If yes tell to make a new question about this.
0
 
LVL 2

Expert Comment

by:bogdan_iuly
ID: 20406035
alex_code,
Sorry for the delay.

Yes, of course it's possible. If you can create a PHP script on the server, then you can use the above procedure to upload files, just as to any other site.
0
 
LVL 9

Expert Comment

by:alex_code
ID: 20410335
Hi bogdan_iuly

~~~~~~~~~~~~~~~~~~~~~~
alex_code,
Sorry for the delay.
~~~~~~~~~~~~~~~~~~~~~~ -->  My friend no problem and thank you that you response.I am just wondering if i can upload an image from my pc to my site without using a php upload script and if yes just tell me to open a new question.


0
 
LVL 2

Expert Comment

by:bogdan_iuly
ID: 20410367
You can use FTP if you want and if you have access. :)
0
 
LVL 9

Expert Comment

by:alex_code
ID: 20410471
----> You can use FTP if you want and if you have access. :) <---- hahaha yes man i know :) i am asking if i can make an application in delphi to do this...
0
 
LVL 2

Expert Comment

by:bogdan_iuly
ID: 20410508
Of course you can, I thought you want something else, since you can find this just by using google.

Here is one example for usign ftp in delphi with indy:
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20920104.html
0
 
LVL 9

Expert Comment

by:alex_code
ID: 20410600
Thanks a lot my friend. Both of your solutions the one with the upload php script and the other with indy i think that it's ok.i 'll give it a try in a few days and i'll post back ;)
1

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to center only a line in richedit? 4 69
Adoquery sql  left join does not work 25 104
Multi-layered image in FireMonkey 9 67
Firemonkey how to swip panel with finger tap 1 18
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question