我有一个PHP文件,它接受来自移动应用程序的两个参数(文本和图像),要处理这些数据,请使用以下命令:
$image = file_get_contents("php://input");
$text = $_POST['Text'];
下一步是通过POST方法将这些数据发送到另一个PHP文件(second.PHP),为此我尝试了以下代码:
$params = array ('Text' => $text);
$query = http_build_query ($params);
$contextData = array (
'method' => 'POST',
'header' => "Connection: close'r'n".
"Content-Length: ".strlen($query)."'r'n",
'content'=> $query );
$context = stream_context_create (array ( 'http' => $contextData ));
$result = file_get_contents (
'second.php', // page url
false,
$context);
但是我也需要发送图片,我该怎么做?
我需要以可以选择的方式发送图像参数来自该命令:
$_FILES['imageUser']
(位于second.php)
您可以将文件上传到临时位置,并将文件的位置+名称POST到second.php
文件中。
例如:
$target_dir = "uploads/";
// If you want unique name for each uploaded file, you can use date and time function and concatenate to the $target_file variable before the basename.
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
// Move the uploaded file
if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)
{
// Now you can post the variable $image
$image = $target_file
}
在second.php
上查询后,您甚至可以执行unlink($image);
来删除文件,这样移动的图像就不会占用服务器上的空间。