2

I am trying to pass an uploaded file as an argument to a python script.

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>

fomrdata.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>";
?>

Create.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]

This works fine and I get the email and everything from the user and the code works if I specify a file name myself but now I want the python script to use the file that the user uploads. And I have absolutely no idea where to start? If it's easier to answer this ignoring the commented out part then that'll work too.

1 Answer 1

2

You can get the filename using

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

From the PHP documentation for tmp_name:

The temporary filename of the file in which the uploaded file was stored on the server.

See the PHP documentation | POST method uploads for more information. You would probably also want to use some of the other fields to make sure an error did not occur prior to calling the Python script.

Does that help?

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

1 Comment

That was so helpful! Thank you so much!

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.