07-03-2020, 11:31 AM
(This post was last modified: 07-03-2020, 02:22 PM by Aaron. Edited 9 times in total.)
We set the path to /tmp/, but found upload file in
/tmp/systemd-private-efff6f88187e49ac8e2197edc20f6ad5-apache2.service-QuWwdx/tmp/.
python code
How to set the correct path for upload file?
/var/www/html/hello_savefile.html
/var/www/cgi-bin/save_file.py
/tmp/systemd-private-efff6f88187e49ac8e2197edc20f6ad5-apache2.service-QuWwdx/tmp/.
python code
Code:
open('/tmp/' + fn, 'wb').write(fileitem.file.read())



/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,)