简体   繁体   English

使用图片将参数发送到另一个PHP文件

[英]Send parameters to another PHP file with image

I have a PHP file that takes two parameters come through a mobile application ( Text and Image ), to treat this data is used the following commands: 我有一个PHP文件,该文件通过移动应用程序( TextImage )传入两个参数,要使用以下命令来处理此数据:

$image = file_get_contents("php://input");
$text = $_POST['Text'];

The next step is to send this data to another PHP file ( second.php ) via POST method, for this I try this code: 下一步是通过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);

However I need to send the image too, how can I do this? 但是我也需要发送图像,该怎么办?

I need to send a image parameter in a way in which I can select it from this command: $_FILES['imageUser'] (which is located in second.php ) 我需要以一种可以从以下命令中选择它的方式发送图像参数: $_FILES['imageUser'] (位于second.php中

You can upload the file to a temp location and POST the file's location+name to second.php file. 您可以将文件上传到临时位置,然后将文件的位置+名称发布到second.php文件中。

For example: 例如:

$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
}

After you query on second.php you can even do unlink($image); second.php查询后,您甚至可以执行unlink($image); second.php to delete the file, so the moved images does not eat space on your server. 删除文件,以便移动的图像不会占用服务器上的空间。

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

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