2014-10-30 65 views
0

当我尝试通过以下我的Python的CGI脚本上传图片文件到“\ tmp目录\”目录下,我不能上传文件到我的“的/ tmp /目录与Python CGI

#!c:/Python/python.exe -u 

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

form = cgi.FieldStorage() 

# Get filename here. 
fileitem = form['filename'] 

# Test if the file was uploaded 
if fileitem.filename: 
    # strip leading path from file name to avoid 
    # directory traversal attacks 
    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> 
<body> 
    <p>%s</p> 
</body> 
</html> 
""" % (message,) 

It gives me this error: 

<type 'exceptions.IOError'>: [Errno 2] No such file or directory: '/tmp/(_)[email protected]_--Soft_3Ovo(_)?.jpg' 
     args = (2, 'No such file or directory') 
     errno = 2 
     filename = '/tmp/(_)[email protected]_--Soft_3Ovo(_)?.jpg' 
     message = '' 
     strerror = 'No such file or directory 

下面是我的选择要上传的文件的HTML代码,我敢肯定没有错误,但我不知道为什么它不工作:

<html> 
<body> 
<form enctype="multipart/form-data" action="save_file.py" method="post"> 
<p>File: <input type="file" name="filename" /></p> 
<p><input type="submit" value="Upload" /></p> 
</form> 
</body> 
</html> 

回答

相关问题