如何解决使用 urllib3 发布图像文件
我正在尝试使用 urllib3 发布图像文件,并且我遵循了官方指南。在远程服务器上,我有一个简单的 php 文件,可以接收图像文件。从 HTML 表单调用时,它已成功运行。我的 Python 代码没有给出任何错误,但也没有传输图像文件。
import urllib3
import certifi
with open('bot-96.png','rb') as fp:
binary_data = fp.read()
http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED',ca_certs=certifi.where())
r = http.request('POST','https://example.com/file_upload.php',body=binary_data,headers={'Content-Type': 'image/png'})
print(r.status)
我做错了什么? php api 是这样写的:
<?php
$target_dir = "/var/sftp/uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry,file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry,your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry,only JPG,JPEG,PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry,your file was not uploaded.";
// if everything is ok,try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
echo "Sorry,there was an error uploading your file.";
}
}
?>
请帮忙。
提前致谢!!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。