-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?

1
  • 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. Commented Aug 24, 2011 at 20:14

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.