user1987508
user1987508

Reputation: 37

Passing an uploaded file to a python script

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.

Upvotes: 2

Views: 2613

Answers (1)

Justin Ethier
Justin Ethier

Reputation: 134157

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?

Upvotes: 2

Related Questions