简体   繁体   English

如何在php中的反引号内编辑参数?

[英]How do I edit arguments within back-ticks in php?

so what I am trying to do is utilize the backtick operator in php to run some python code that will display on my Apache Localhost. 所以我想做的是利用php中的backtick运算符来运行一些显示在我的Apache Localhost上的python代码。 This python code contains the ability to edit arguments, but when I tried to edit the arguments, nothing came out on my localhost website. 这段python代码包含编辑参数的功能,但是当我尝试编辑参数时,我的本地主机网站上什么都没有。

My code: 我的代码:

<!DOCTYPE html>
<html>
<head>
  <title>Upload your files</title>
</head>
<body>
  <form enctype="multipart/form-data" action="upload.php" method="POST">
    <p>Upload your file</p>
    <input type="file" name="uploaded_file"></input><br />
    <input type="submit" value="Upload"></input>
  </form>
</body>
</html>
<?PHP
  if(!empty($_FILES['uploaded_file']))
  {
    $path = "uploads/";
    $path = $path . basename( $_FILES['uploaded_file']['name']);
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
      echo "The file ".  basename( $_FILES['uploaded_file']['name']). 
      " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
  }
?>
<?PHP 
   $python = `python classify_image.py --image_file C:\xampp\htdocs\uploads\bamboo.jpg`;
   echo $python;
?>

This is what I see in Localhost: 这是我在Localhost中看到的: 用修改后的参数

But when I remove the " --image_file C:\\xampp\\htdocs\\uploads\\bamboo.jpg" from the backticks, the python code reverts to reading the default image and returns this on my localhost: 但是,当我从反引号中删除“ --image_file C:\\ xampp \\ htdocs \\ uploads \\ bamboo.jpg”时,python代码将还原为读取默认图像,并将其返回到我的本地主机: 没有修改的参数

Can someone tell me what is wrong with my use of the backtick operator in php? 有人可以告诉我在PHP中使用反引号运算符有什么问题吗?

You can use a variable inside the backticks. 您可以在反引号内使用变量。

You should also put the classify_image.py call inside the if , so you only do it when the file has been successfully uploaded. 您还应该将classify_image.py调用放在if ,因此只有在文件成功上传后才执行此操作。

And use forward slashes in the pathname rather than backslashes, or double the backslashes. 并在路径名中使用正斜杠而不是反斜杠,或者将反斜杠加倍。 In double quotes and backticks, backslash is an escape prefix, resulting in incorrect parsing of the pathname. 在双引号和反引号中,反斜杠是转义前缀,导致对路径名的解析不正确。

<?PHP
  if(!empty($_FILES['uploaded_file']))
  {
    $path = "uploads/";
    $path = $path . basename( $_FILES['uploaded_file']['name']);
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
      echo "The file ".  basename( $_FILES['uploaded_file']['name']). 
      " has been uploaded";
      $python = `python classify_image.py --image_file C:/xampp/htdocs/$path`;
      echo $python;
    } else{
        echo "There was an error uploading the file, please try again!";
    }
  }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM