1

I have a simple PHP form which allows user to selet a file for image processing as well as a Java program to generate some imagesas output. The PHP will make a copy of the image in a specified folder but I need to pass in the image name from PHP to Java in order for the java program to run for the uploaded file. Is there any way to do it? These are part of my current codes for PHP...

<form method="post" action="imageview.php" enctype="multipart/form-data">
Select Image File to Upload: <input type="file" name="image" />
<br><input type="submit" name="upload" value="Upload File!">
</form>

<?php
if (file_exists("photosmp/".$imagename))
{
    echo $imagename." already exists.";
    echo "It is stored in ".$imagetmpname;
}
else
{
    $newfilename = 'duplicate_'.$imagename;
    move_uploaded_file($imagetmpname,"photosmp/". $newfilename);
    $image = "photosmp/$newfilename";
}
?>
2
  • How are you calling Java from PHP? Commented Aug 11, 2014 at 5:24
  • echo exec ("java PROG"); Commented Aug 11, 2014 at 5:28

2 Answers 2

2

Call your Java program by passing a command line parameter:

echo exec ('java PROG "' . $image . '"'); 
Sign up to request clarification or add additional context in comments.

5 Comments

is there any way to change this code in order to let the image file from PHP to be taken into this Java prog? FileDialog fd = new FileDialog(new Frame(), "Open",FileDialog.LOAD); fd.setFile("*.jpg"); fd.setDirectory(".\\"); fd.setLocation(50, 50); fd.setVisible(true); String FilePath = fd.getDirectory(); String FileName = fd.getFile(); String imageUpload = FilePath + FileName; String imageCopy = FilePath + "copy_" + FileName ;
You must be having a main(String[] args) method in Java. Just access argv[1] as your image name which is being passed from command line using exec function of PHP.
may i know the way to go about it? cause im new in Java... this is my main(String[ args) method public static void main(String[] args) throws IOException { FileDialog fd = new FileDialog(new Frame(), "Open...", FileDialog.LOAD); fd.setFile("*.jpg"); fd.setDirectory(".\\"); fd.setLocation(50, 50); fd.setVisible(true); String FilePath = fd.getDirectory(); String FileName = fd.getFile(); String imageUpload = FilePath + FileName; String imageCopy = FilePath + "copy_" + FileName ;
See I cannot understand code from comments section but as I suggested you can use String imageName = ''; if (args.length>1) imageName = args[1]; in your Java code and use imageName variable for your processing.
Glad to know it worked out, can you mark the answer as accepted by clicking on tick mark on top-left of my answer.
0

There are various system command are avalaible to fork a java program, like

  • exec(), system(), pctl etc

You can use these.

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.