【问题标题】:Passing an uploaded file to a python script将上传的文件传递给 python 脚本
【发布时间】:2013-01-07 10:00:05
【问题描述】:

我正在尝试将上传的文件作为参数传递给 python 脚本。

form.php

<html>
<body>
<form method="post" enctype="multipart/form-data" action = "formdata.php">

<label for="file">Upload File:</label>
<input type="file" name="file" id="file" />
<label for="user_email">Email:</label>
<input type="text" name="user_email" id="user_email" />
<input type="submit" id="submit" value="Submit" />

</form>
</body>
</html>

formrdata.php

<?php
$email = $_POST['user_email'];
$uploaded_file = //don't know what to do here

$output = shell_exec("/usr/bin/python /var/www/Create.py $uploaded_file $email);
echo "<pre>$output</pre>";
?>

创建.py

  #    def moveMetadataFile():
  #      import shutil
  #      shutil.copyfile('/var/www/**uploaded_file**', '/var/www/temp/**uploaded_file**')
  # some more code
  upload = sys.argv[1]
  email = sys.argv[2]

这很好用,如果我自己指定文件名,我会收到用户的电子邮件和所有内容,并且代码可以正常工作,但现在我希望 python 脚本使用用户上传的文件。我完全不知道从哪里开始? 如果忽略注释掉的部分更容易回答这个问题,那也可以。

【问题讨论】:

    标签: php python upload


    【解决方案1】:

    您可以使用获取文件名

    $uploaded_file = $_FILES['file']['tmp_name'];
    

    来自tmp_name 的 PHP 文档:

    上传文件存储在服务器上的文件的临时文件名。

    请参阅PHP documentation | POST method uploads 了解更多信息。您可能还想使用其他一些字段来确保在调用 Python 脚本之前没有发生错误。

    这有帮助吗?

    【讨论】:

    • 这太有帮助了!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    相关资源
    最近更新 更多