Solved

Upload file to web server

Posted on 2007-11-15
17
7,951 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 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