简体   繁体   English

检测传入的 PHP 帖子,保存文件

[英]Detect incoming PHP post, save file

Currently there is a server set up to post zlib files to another server : (Here is the C# code for that)目前有一个服务器设置为将 zlib 文件发布到另一台服务器:(这是 C# 代码)

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://server-ip-here/postlistener?filename=filename.zlib");
req.UseNagleAlgorithm = true;
req.AllowWriteStreamBuffering = true;
req.Method = "POST";
req.Accept = "application/xml";
req.ServicePoint.Expect100Continue = false;
System.Net.ServicePointManager.CheckCertificateRevocationList = false;
req.Proxy = new WebProxy();

filename = "filename.dat";
byte[] postData = File.ReadAllBytes(filename);
Stream stream = req.GetRequestStream();
stream.Write(postData, 0, postData.Length);
stream.Flush();
stream.Close();

req.BeginGetResponse(new AsyncCallback(responseHandler), this);

Need to write some php on a different server so that when this specific IP posts stuff to me (aka when server 192. something or other posts to my server IP) I need to be able to需要在不同的服务器上编写一些 php 以便当这个特定的 IP 向我发布内容时(也就是当服务器 192. 某些东西或其他发布到我的服务器 IP 时)我需要能够

  • a) Detect that a POST is being made a) 检测是否正在进行 POST
  • b) Check it is by a whitelisted IP b) 检查是否通过列入白名单的 IP
  • c) Download the POST data (which should be binary) and save it as whatever its name is in the post (need to accept filename in URL: . www.blah.com/upload?filename=somefile_1234_12389126496129684790.zlib) c) 下载 POST 数据(应该是二进制的)并将其保存为帖子中的任何名称(需要接受 URL 中的文件名:.www.blah.com/upload?filename=somefile_1234_12389126496129684790.zlib)

However being new ish to PHP this is what my research so far has told me:然而,作为 PHP 的新手,这是我迄今为止的研究告诉我的:

  • a) Use this code to see if the request is a post a) 使用此代码查看请求是否为帖子

    if '($_SERVER['REQUEST_METHOD'] === 'POST') { // … }

  • b) use: $_SERVER['HTTP_REFERER'] b) 使用: $_SERVER['HTTP_REFERER']

  • c) No real idea. c) 没有真正的想法。

Also not entirely sure how to string these parts together.也不完全确定如何将这些部分串在一起。 Any ideas how I can go about listening for POSTs being sent from that IP, then downloading the file that is being posted?我有什么想法可以监听从该 IP 发送的 POST,然后下载正在发布的文件?

Also really not sure how to set my php page up to be able to do:也真的不知道如何设置我的 php 页面才能做到:

/postlistener?filename=filename.zlib

I believe the postlistener part is my page, and somehow that page is supposed to get a file also?我相信 postlistener 部分是我的页面,不知何故该页面也应该获得一个文件? TH=his is my understanding, have taken over this project for someone else in the company, and of course there is no documentation to go with it. TH=他是我的理解,已经为公司的其他人接手了这个项目,当然没有文件可以配合。 Any help would be big help, thanks.任何帮助都会有很大帮助,谢谢。

This will listen for any POST data, and then put the post variable name into an array.这将侦听任何 POST 数据,然后将 post 变量名称放入一个数组中。

    <?

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
     if($_SERVER['REMOTE_ADDR'] == '*IPADDRESS*') {
          $posts = array();
          foreach ($_POST as $key => $value){
               echo "{$key} = {$value}\r\n";
               array_push($posts,$key);
          }
      }
}

print_r($posts);
?>

After that you can loop through the $posts array and download all the things.之后,您可以遍历$posts数组并下载所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM