将参数发送到另一个带有图像的PHP文件


Send parameters to another PHP file with image

我有一个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);来删除文件,这样移动的图像就不会占用服务器上的空间。