2

I have a python script using mechanize to upload an image to a php script. The problem is that the image is 3,000kb yet only 52kb is showing on the server.

HERE IS THE PYTHON:

from mechanize import Browser
br = Browser()
br.open("http://www.mattyc.com/up")
br.select_form(name="upper")
br.form.add_file(open("tester.jpg"), 'image/jpeg', "tester.jpg")
br.submit()

HERE IS THE WEB PAGE:

<?php
if (move_uploaded_file($_FILES['file']['tmp_name'], $_FILES["file"]["name"])) {
    $success_msg = "GOOD";
    echo $success_msg;
}else{
echo "ERROR";
}


?>
<html>
<head>
<title>UP</title>
</head>
<body>
<form action="up.php" method="post" enctype="multipart/form-data" name="upper" >
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
1
  • 1
    can you please post the output of $_REQUEST, $_GET, $_FILES and $_POST in your PHP-Script? Commented Feb 2, 2011 at 13:19

2 Answers 2

1

Most likely, it's the PHP ini setting limiting the file upload size (upload_max_filesize).

Edit: to check this setting, you can use: echo ini_get('upload_max_filesize');. If the size in there is 52KB, then you have your answer. Actually, if it's anything less than the size of the file you want to upload, then raise it, because that will definitely become a problem somewhere down the line.

Sign up to request clarification or add additional context in comments.

5 Comments

it printed 8M. So im guessing thats not the problem?
Apparently not from within PHP, but there still could be something on that end (Apache/etc) interrupting things. Go ahead and take @schneck's advice and post some more info from the output of the superglobals. $_FILES may have the most pertinent information.
How would i do that. Im somewhat new.
It would be a good idea to read the doc on it. Note: that link jumps to a comment that demonstrates the fomat of $_FILES when dumped out. Sub-indeces for [size] and [error] may be of particular value (you're hoping for UPLOAD_ERR_OK).
the php seems to be fine when i visit the script via real browser the upload work great so i guess its the python part
1

The solution is to have the file open in binary, rather than have it open in plain text mode. In your python code, replace the relevant line with:

br.form.add_file(open("tester.jpg" ,"rb"), 'image/jpeg', "tester.jpg")

The simple addition of the "rb" flag (read binary) will fix your problem. The filsize was shrunk down because it attempted to read the file normally, and uploaded only the characters that were in the ascii plain text range.

Enjoy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.