Setting a Limit to the Maximum Size of an Upload

J

Joey C.

Hello,
I'm designing a small "briefcase" program that will allow me to quickly
upload, download, and delete files in a briefcase. The only real
things that I have left to do are to design a method for checking if
the file exists, preventing it from overwriting files from other
directories, and setting a user-configurable maximum limit on the
file's size. The former two, I can handle by myself with no problem.
However, the I'm having a little trouble with.

thefile = params["upfile.file"]
if os.path.getsize(thefile) <= conf["upmax"]:
print "File Size Okay." #Add Functions Later...
else:
print "File Too Large." #Here, too.

CGItb reported the following error:
TypeError: coercing to Unicode: need string or buffer, instance found
args = ('coercing to Unicode: need string or buffer, instance
found',)

This seems like an incredibly simple problem, but I just can't seem to
wrap my mind around it. Thank you for your help.
 
F

Fredrik Lundh

Joey C. said:
thefile = params["upfile.file"]
if os.path.getsize(thefile) <= conf["upmax"]:
print "File Size Okay." #Add Functions Later...
else:
print "File Too Large." #Here, too.

CGItb reported the following error:
TypeError: coercing to Unicode: need string or buffer, instance found
args = ('coercing to Unicode: need string or buffer, instance
found',)

cgitb probably also reported what line you got that error for, and the values
of the variables involved. can you perhaps post that information too?

</F>
 
J

Joey C.

Here is a basic overview of the variables included there.

params = cgi.FieldStorage()
I accidentally made a mistake when typing what the "thefile" variable
is.
thefile = params["upfile"].file
"upfile" is the CGI field that contains the file that I'm uploading.
As you can see, the if statement just compares two values,
os.path.getsize(thefile) and conf["upmax"], a variable I set that is
designated as the maximum file size allowed.

I'm assuming that this is all the information you need. I'm sorry for
not including it earlier; I was in a bit of a rush. ^.^
 
F

Fredrik Lundh

Joey C. said:
Here is a basic overview of the variables included there.

params = cgi.FieldStorage()
I accidentally made a mistake when typing what the "thefile" variable
is.
thefile = params["upfile"].file
"upfile" is the CGI field that contains the file that I'm uploading.
As you can see, the if statement just compares two values,
os.path.getsize(thefile) and conf["upmax"], a variable I set that is
designated as the maximum file size allowed.

I'm assuming that this is all the information you need. I'm sorry for
not including it earlier; I was in a bit of a rush. ^.^

and I'm sorry for not noticing the error: os.path.getsize takes a filename,
but the file attribute contains a file handle, and that UnicodeError is what
you get if you pass the wrong thing to os.path.getsize. e.g:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\Python23\lib\ntpath.py", line 228, in getsize
return os.stat(filename).st_size
TypeError: coercing to Unicode: need string or buffer, NoneType found

(here, the full traceback reveals that the problem is inside getsize, and not
in the comparision. the error message itself isn't exactly helpful, though...)

the file handle is usually a real (but temporary) file, so you should be able to
get the size by seeking to the end of the file:

file = params["upfile"].file
file.seek(0, 2)
size = file.tell()
file.seek(0) # rewind

inspecting the headers attribute might also help.

</F>
 
J

Joey C.

I'm afraid on my interpreter, this works.

<= 1000.

No problems there, as you can see.
 
F

Fredrik Lundh

Joey C. said:
I'm afraid on my interpreter, this works.


<= 1000.

No problems there, as you can see.

I'm not sure if you replied to my post, but what I tried to say is that

params["upfile"].file

doesn't return a string, it returns a file object. and os.path.getsize
on something that's not a string gives the TypeError you're seeing.

</F>
 
J

Joey C.

Oh, I'm sorry, I didn't understand what you meant at first.
Then I read your reply over again and noticed that you said that the
problem lied in os.path.getsize() when I tried to run it on the
contents of an open file.

I'll try the method you outlined now.
 
J

Joey C.

Yes, I see that now. I tried your method and it seemed to work fine
until I tried printing the filesize out.

def checkfilesize(thefile):
# Check the Size of the File
global filesize
thefile.seek(0,2)
filesize = thefile.tell()
thefile.seek(0)
print filesize
print conf["upmax"]
if filesize <= conf["upmax"]:
print "File Size Okay."
noupload = False
else:
print "File is too Large."
noupload = True

Basically conf["upmax"] is a number that I extract from a configuration
file that is the maximum size of an upload. I had tried setting
conf["upmax"] to 1 and it should have technically disallowed a 28 byte
file to pass through. But it did, so I added two lines to print the
file size and the conf["upmax"] variable.
The file size turned out to be "0"!
thefile still is just params["upfile"].file, by the way.

Any suggestions?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top