Upload file to web server

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
alpiresAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
bogdan_iulyConnect With a Mentor Commented:
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
 
mokuleCommented:
Hi,
    Strm.Position := 0;
Just before Post call
1
 
alpiresAuthor Commented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
mokuleCommented:
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
 
alpiresAuthor Commented:
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
 
mokuleCommented:
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
 
alpiresAuthor Commented:
My server dont accept upload files directly
0
 
AlexSoftware EngineerCommented:
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
 
AlexSoftware EngineerCommented:
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
 
bogdan_iulyCommented:
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
 
AlexSoftware EngineerCommented:
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
 
bogdan_iulyCommented:
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
 
AlexSoftware EngineerCommented:
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
 
bogdan_iulyCommented:
You can use FTP if you want and if you have access. :)
0
 
AlexSoftware EngineerCommented:
----> 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
 
bogdan_iulyCommented:
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
 
AlexSoftware EngineerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.