0

I've been getting this problem for a little while now.
The thing is that i have an HTML form proceed by PHP which looks like that:

<?php
if (isset($_POST)) {
    $link = mysqli_connect("localhost", "root", "password", "tablename");
    $url = $_POST['url'];
    $shorturl = str_replace(['/', 'https:', 'http:'], '', $url);
    $iconpath = shell_exec("python3 /home/favicon.py {$url}");
    $sql = "INSERT INTO links (url, shorturl, iconpath) values ('{$url}', '{$shorturl}', '{$iconpath}')";
    $result = mysqli_query($link, $sql);
}
/*
header('Location: /');
exit;
*/
?>

This code is supposed to extract the given URL from the form, execute python file that downloads favicon of the site and insert the data into a MySQL table.
Python code:

import sys
import requests

url = sys.argv[1]
img_data = requests.get(f'{url}/favicon.ico').content
iconpath = '/home/favicon.png'
with open(iconpath, 'wb') as handler:
    handler.write(img_data)
print(iconpath)


It all works fine except for the favicon part. When i run the python file from shell, it works fine. When i run it from PHP interactive console by the exact same code written in the form handler, it works fine. But it just won't run properly in the handler, because even when i try to debug it like

echo $iconpath;

it doesn't actually show anything in there. What can i do with it?

P.S. the code actually inserts $url and $shorturl variables into the database, so i said it all works fine except the shell_exec part

7
  • 1
    Does this answer your question? Call python script with php (windows xampp server) Commented Aug 5, 2022 at 19:57
  • @MarkusZeller it doesn't. as i said, the code totally works in PHP interactive console, but won't work in the handler Commented Aug 5, 2022 at 20:02
  • 2
    Looks like a permissions issue. The user that your web server runs as (which will be different from you using the command line) doesn't have the right permissions to run the Python code. That said, it's such a simple piece of Python code, why not do it all in PHP? Commented Aug 5, 2022 at 20:06
  • The error as to why it doesn't run likely shows up in your server logs. Commented Aug 5, 2022 at 20:15
  • 1
    In addition to what @TangentiallyPerpendicular said about permissions, you have a PATH variable set, the web server likely does not. Specify the full path to python3 A more accurate duplicate target is possibly this one, but I'm out of close votes: stackoverflow.com/questions/5456961/… Commented Aug 5, 2022 at 20:31

0

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.