Skip to main content
 首页 » 编程设计

php之$_FILES 在使用 python 脚本发布后为空

2025年05月04日16pengyingh

我有一个 python 脚本,应该将文件上传到 php 脚本。

python

import requests 
 
file={'file':('text.txt','hello')} 
 
url='mywebsite.org/test.php 
 
 
response = requests.post(url, files=file) 
                               
print(response.text) 

PHP

<?php 
    var_dump($_FILES); 
    var_dump($_POST); 
?> 

这是我对 python 脚本的响应:

array(0) {

}

array(0) {

}

但是,当我尝试发布到 http://httpbin.org/post 时,

我明白了

...

"files": {

"file": "hello"

},

...

这似乎表明我的服务器有问题。可能是什么问题?

请您参考如下方法:

您的 python 代码似乎有问题 - 目前您没有发送文件,因为它没有打开。假设您的 text.txt 包含 1234。发布到 http://httpbin.org/post像这样:

import requests 
 
file={'file':(open('text.txt','r').read())} 
 
url='http://httpbin.org/post' 
 
response = requests.post(url, files=file) 
 
print(response.text) 

我们得到以下响应:

... 
"files": { 
    "file": "1234" 
  }, 
... 

如果你想添加一些额外的参数,你可以这样做:

values = {'message': 'hello'} 
response = requests.post(url, files=file, data=values)