python 未正确上传到 php
我有一个使用 mechanize 将图像上传到 php 脚本的 python 脚本。问题是图像有 3,000kb,但服务器上仅显示 52kb。
这是 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()
这是网页:
<?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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最有可能的是 PHP ini 设置限制了文件上传大小 (upload_max_filesize)。
编辑:要检查此设置,您可以使用:
echo ini_get('upload_max_filesize');
。如果其中的大小是 52KB,那么您就有答案了。实际上,如果它小于您要上传的文件的大小,请提出它,因为这肯定会成为某个地方的问题。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.解决方案是以二进制方式打开文件,而不是以纯文本模式打开文件。在您的 python 代码中,将相关行替换为:
简单添加
"rb"
标志(读取二进制)将解决您的问题。 filsize 被缩小,因为它尝试正常读取文件,并且仅上传 ASCII 纯文本范围内的字符。享受。
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:
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.