Solved

Upload file to web server

Posted on 2007-11-15
17
7,791 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now