How to get image / png content via PHP

This is my first post for, so please forgive me if I used the wrong formatting or conventions. I am trying to write a test script that receives a png image from a POST web page (multipart / form-data), cuts out the image that was sent, and forwards it to a third party as Content-Type: image / png.

I have a php file (catcher.php) that is the recipient of the forwarded image. Post.php, the php file that sends the uploaded image to catcher.php is located below:

<?php $img = imagecreatefrompng($_FILES['myfile']['tmp_name']); imagepng($img); $opts = array( 'http'=>array( 'method'=>"POST", 'content'=>$img, 'header'=>"Content-Type: image/png\r\n" ) ); $context = stream_context_create($opts); file_get_contents( "http://localhost/catcher.php", false, $context); ?> 

Post.php retrieves the file as accurately as possible from the web page, which places it as multipart / form-data, however I'm not sure how to access the contents of image / png in catcher.php.

My question is: in catcher.php, how do I access the contents of the image? I tried $ _POST ['content'] and I get the following error: "Undefined index: content". Therefore, I know that I'm just not looking for the right data. Is there a special supergroup variable like $ _POST or $ _REQUEST that I can use to access the hosted image content, or is there some other solution?

Solution I managed to find the result I was looking for, with the following code for catcher.php:

 $input = fopen("php://input","+r"); $destination = fopen($target_path, 'w'); stream_copy_to_stream($input, $destination); fclose($input); fclose($destination); ?> 

Thanks to both Mark and Brendan for your helpful answers!

+4
source share
2 answers

imagepng($img) performs immediate output of binary garbage containing PNG. it does not fall into a variable. What you are actually posting is a line that probably says β€œGD Resource # 7” or something similar.

The entire imagepng () bit is useless anyway - you decode PNG into a representation in memory, and then try to transcode into PNG again. A waste of memory since the file is already on disk. You can do everything with:

 <?php if ($_FILES['myfile']['error'] !== UPLOAD_ERR_OK) { die("Upload failed with error code " . $_FILES['myfile']['error']); } $opts = array( 'http'=>array( 'method'=>"POST", 'content'=> file_get_contents($_FILES['myfile']['tmp_name']), 'header'=>"Content-Type: image/png\r\n" ) ); $context = stream_context_create($opts); file_get_contents( "http://localhost/catcher.php", false, $context); 

Note the addition of a download success check - assuming that a successful download will result in sorrow along the line.

However, you also do not properly load the POST file. To do this, you need to actually simulate a full-sized HTML form with <input type="file"> present and enctype="multipart/form-data" and something else. All you do is send the raw PNG to the server.

You can probably get it by reading php://input in your script trap.

+3
source

The problem is that your data is not encoded as multipart / form-data , it is just PNG.

You have two options:

  • Encode the data correctly for POST - then you can read it with $_POST or $_REQUEST .
  • Save post.php how to do this and read the raw data from php: // input to catcher.php .

For option 1, it looks like HttpRequest handles the encoding of the form for you.

+1
source

Source: https://habr.com/ru/post/1398097/


All Articles