-2

I require to upload a file using a PHP page into a Java Servlet. The Java Servlet uses the HTTP Post for uploading the files. Is there a way to do this. Could you please advice on how this is done. I am pretty new to uploading files in PHP and would like to understand the concept and also how my above functionality can be achieved. I right now dont have any code that I can look at putting here for someone to help me as I am starting a fresh on PHP file uploads. Also can i acheive this by using jquery.post?

Abishek
  • 11,191
  • 19
  • 72
  • 111
  • 1
    Just to clarify, you have a PHP page in which there is a `form` and on submit of the form, you want to submit the form to a java servlet that will do the upload? If this is true BalusC's answer below is what you need. If you need it to look like AJAX, then do it with an inner frame. – Ali Aug 24 '11 at 20:14

2 Answers2

3

PHP is not relevant. It's all about its generated HTML output. Just make sure that the PHP-generated form look like this (open page in browser, rightclick and View Source to verify it):

<form action="http://example.com/uploadservlet" method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" />
</form>

For the servlet end, just check How to upload files to server using JSP/Servlet?

If you really intend to use JavaScript to send the POST request asynchronously, checkout the jQuery Form plugin which simulates asynchronous file uploads by an <iframe> hack, or HTML5/XHR2 which supports asynchronous file uploads natively.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • so, the upload will happen even if the servlet is on another domain. For example my php is hosted on "www.abc.com" and my servlet is on "www.xyz.com". I can still use this method to upload the files? – Abishek Aug 24 '11 at 20:12
  • 1
    Yes. Didn't you even *try* it yet? Only with cross-domain XHR1 it is not possible if the other side doesn't set `Access-Control-Allow-Origin` header, but it's not possible to upload files with XHR1 anyway. – BalusC Aug 24 '11 at 20:28
0

No, jquery/ajax cannot upload files. You can use curl: http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html

Marc B
  • 356,200
  • 43
  • 426
  • 500