如何使用Python $ _FILES

时间:2019-01-11 13:21:40

标签: python python-requests

我有PHP手册中的以下示例表格:

<form action="file-upload.php" method="post" enctype="multipart/form-data">
  Send these files:<br />
  <input name="userfile[]" type="file" /><br />
  <input name="userfile[]" type="file" /><br />
  <input type="submit" value="Send files" />
</form>

PHP脚本会这样读取:

$_FILES['userfile']['name'][0] - filename
$_FILES['userfile']['size'][0] - filesize
$_FILES['userfile']['type'][0] - mime type

如何以这种格式上传单个文件?

当我尝试这样做时:

files = {
    'userfile': (
        'file.jpg',
        open('file.jpg', 'rb'),
        'image/jpeg'
    )
}
requests.post(upload_url, files=files)

PHP这样接收它:

$_FILES['userfile']['name'][0] == "f" - first char from 'file.jpg'
$_FILES['userfile']['size'][0] == 0
$_FILES['userfile']['type'][0] == "i" - first char from 'image/jpeg'

0 个答案:

没有答案