Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7558423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:22:10+00:00 2026-05-30T12:22:10+00:00

This is my first post to Stack Overflow so please forgive me if I

  • 0

This is my first post to Stack Overflow so please forgive me if I have used incorrect formatting or conventions. I am trying to write a test scenario which receives a png image from a webpage POST (multipart/form-data), strips out the image which was posted and forwards that on to a 3rd party as Content-Type: image/png.

I have a php file (catcher.php) which is the recipient of the forwarded image. Post.php, the php file that posts the uploaded image to catcher.php is 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 gets the file just fine from the webpage that posts it as multipart/form-data, however I am unsure how to access the image/png content in catcher.php.

My question is, in catcher.php, how do I access the image content? I have tried $_POST[‘content’] and I obtain the following error: “Undefined index: content”. So I know I am just not looking for the correct data. Is there a specific superglobal variable such as $_POST or $_REQUEST that I can use to access the posted image content, or is there some other solution?

RESOLUTION
I was able 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);
?>

Thank you both Marc and Brenden for your expedient responses!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:22:11+00:00Added an answer on May 30, 2026 at 12:22 pm

    imagepng($img) does an immediate output of the binary garbage comprising the PNG. it’s not captured into a variable. What you’re actually posting is a string that probably says “GD Resource #7” or something similar.

    The whole imagepng() bit is useless anyways – you’re decoding a PNG into an in-memory representation, then trying to re-encode to PNG again. A pointless waste of memory, since the file is already on disk. You could do the whole thing 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 checking for upload success – assumingn an upload succeeded will cause you grief down the line.

    That being said, you’re also not doing a proper POST file upload. For that to work, you have to actually simulate a full-blown HTML form with <input type="file"> present, and enctype="multipart/form-data" and whatnot. All you’re doing is sending a raw PNG to the server.

    You can probably retrieve it by reading from php://input in your catcher script.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first post on Stack Overflow. I am trying to build a
Hello everyone this is my first post on stack overflow.com I am trying to
This is my first post on Stack Overflow so please exuse (and feel free
This is my first post on Stack Overflow and I'm just wondering on the
This is my first post on Stack, so please bear with me if I
This is my first post on StackOverflow, so please be gentle... I have some
this is my first post on stack overflow! I'm new to Java and I've
This is my first post to stack overflow,... :) I like this site a
This is my first post to stack overflow. I've been lurking this site for
This is my very first post on Stack Overflow, so I hope I don't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.