Tinker Board Forum

Full Version: How to set CGI upload path?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
We set the path to /tmp/, but found upload file in 
/tmp/systemd-private-efff6f88187e49ac8e2197edc20f6ad5-apache2.service-QuWwdx/tmp/.

python code
Code:
open('/tmp/' + fn, 'wb').write(fileitem.file.read())
How to set the correct path for upload file? Huh Huh Huh

/var/www/html/hello_savefile.html
Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Aaron Test</title>
</head>
<body>
<form enctype="multipart/form-data"
                    action="/cgi-bin/save_file.py" method="post">
  <p><input type="file" name="filename" /></p>
  <p><input type="submit" value="Upload" /></p>
  </form>
</body>
</html>

/var/www/cgi-bin/save_file.py
Code:
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, cgi
import cgitb; cgitb.enable()

fs = cgi.FieldStorage()
fileitem = fs['filename']      

# Test if the file was uploaded
if fileitem.filename:
  fn = os.path.basename(fileitem.filename)
  open('/tmp/'+ fn, 'wb').write(fileitem.file.read())
  message = 'The file "' + fn + '" was uploaded successfully'
else:
  message = 'No file was uploaded'

print """\
Content-Type: text/html\n
<html>
<head>
<meta charset="utf-8">
<title>Aaron Test</title>
</head>
<body>
  <p>%s</p>
</body>
</html>
""" % (message,)
I found a way to set the path is cgi-bin/upload

python code
Code:
open('upload/' + fn, 'wb').write(fileitem.file.read())

Code:
$sudo mkdir /var/www/cgi-bin/upload
$sudo chmod 777 /var/www/cgi-bin/upload