1

我正在尝试重命名我通过 http POST 接受的文件。请看代码:

<?php
$xmlData = fopen('php://input' , 'rb');
while (!feof($xmlData)) { $xmlString .= fread($xmlData, 4096); }
fclose($xmlData);

file_put_contents('temp/message' . date('m-d-y') . '-' . time() . '.xml', $xmlString, FILE_APPEND);

$xml = new SimpleXMLElement($xmlString);

$id = trim($xml->MSG->ID);
$receiver = trim($xml->MSG->RECEIVER);
$message = trim($xml->MSG->MESSAGE);
$sender = trim($xml->MSG->SENDER);
$binary = trim($xml->MSG->BINARY);
$sent = trim($xml->MSG->SENT);

foreach ($xml->{'line-items'}->{'line-item'} as $lineItem) {
  array_push($messageTitles, trim($lineItem->title));
}

header('HTTP/1.0 200 OK');
exit();

现在我对如何重命名这个有点不知所措?

4

1 回答 1

3

您甚至不需要保存文件来处理 XML 树。所以你可以处理文件并file_put_contents(...)在最后移动。

<?php
$xmlData = fopen('php://input' , 'rb');
while (!feof($xmlData)) { $xmlString .= fread($xmlData, 4096); }
fclose($xmlData);

$xml = new SimpleXMLElement($xmlString);

$id = trim($xml->MSG->ID);
$receiver = trim($xml->MSG->RECEIVER);
$message = trim($xml->MSG->MESSAGE);
$sender = trim($xml->MSG->SENDER);
$binary = trim($xml->MSG->BINARY);
$sent = trim($xml->MSG->SENT);

foreach ($xml->{'line-items'}->{'line-item'} as $lineItem) {
  array_push($messageTitles, trim($lineItem->title));
}

file_put_contents("temp/$receiver.xml", $xmlString, FILE_APPEND); // warning: security issue here

header('HTTP/1.0 200 OK');
exit();

请注意,您应该强制执行安全限制,以防止用户使用任意名称命名您的文件。

于 2013-07-30T12:50:34.577 回答