-2

i am working on a project that detects facial expressions in python.but i have to provide image to this code through php.The following php code saves image in directory.how can i call the following code in html button.

<?php
     function fun(){

    $img = $_POST['image'];
    $folderPath = "C:/xampp/htdocs/";

    $image_parts = explode(";base64,", $img);
    $image_type_aux = explode("image/", $image_parts[0]);
    $image_type = $image_type_aux[1];

    $image_base64 = base64_decode($image_parts[1]);
    $fileName = '1'. '.jpeg';

    $file = $folderPath . $fileName;
    file_put_contents($file, $image_base64);

    print_r($fileName);
    $command = escapeshellcmd("python C:/xampp/htdocs/generate_graph.py");
    $output = shell_exec($command);



 }
 ?>
2
  • 2
    There are two main methods to connect to a server from a HTML page, a form element and an AJAX call (= XMLHttpRequest). Commented Nov 19, 2019 at 5:32
  • You should be able to do this via hyperlinking this PHP script URL on that button via <a></a> tag Commented Nov 19, 2019 at 12:48

1 Answer 1

1

HTML form should be like the blow if your form and PHP code are on the same page.

<form action="" method="POST">
#Updading based on comment 
     <button type="submit" class="">Submit</button>
</form>

Remove the function fun(){}

Add the following :

if(isset($_POST)){

  $img = $_POST['image'];
    $folderPath = "C:/xampp/htdocs/";

    $image_parts = explode(";base64,", $img);
    $image_type_aux = explode("image/", $image_parts[0]);
    $image_type = $image_type_aux[1];

    $image_base64 = base64_decode($image_parts[1]);
    $fileName = '1'. '.jpeg';

    $file = $folderPath . $fileName;
    file_put_contents($file, $image_base64);

    print_r($fileName);
    $command = escapeshellcmd("python C:/xampp/htdocs/generate_graph.py");
    $output = shell_exec($command);
}

Since your action is empty then it will hit the current page and if condition will work because of form submitting as POST method.

Hope it will help.

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

5 Comments

i add button in form tag .whenever i clicks on button it do nothing.
I donot know much about php.i think i am putting this code in wrong place.I am still getting no response on button click.i am putitng the given php code after </form>tag .is it right place?
could you please dump the whole code in your question?
it is working.I have another question.I have to run this code after every 10 seconds.How can i do that?
You've to use javascript for your last question.

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.