Solved

Upload file to web server

Posted on 2007-11-15
17
7,889 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
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…

838 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