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 7043069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:14:40+00:00 2026-05-28T02:14:40+00:00

i have a question about fopen() and base64 comunication. The scenario is that: i

  • 0

i have a question about fopen() and base64 comunication.
The scenario is that: i have a service A that must fetch from url a resource (png/jpeg or pdf). The code is that:

    $uri = urldecode($_POST['uri']);
    $imgfile = $uri;
    $handle  = fopen($uri, 'r'); 
    $imagebinary = '';

    while (!feof($handle)) {
        $c = fgetc($handle);
        if($c === false) break;
        $imagebinary .= $c;
    }
    fclose($handle);
    $return =  base64_encode($imagebinary);

Now i have JQUERY function that send this $return (something like that: ‘iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAAAXNSR0IArs’) to another PHP service, named B.
B service take this string and try to save it on disk. In the specific the service B try to save the file on amazon s3, the code is that:

    // in $imagedata is saved the string generated by service A
    $imagedata = $_POST['serviceA_base64encodedfile'];
    // $contentType taken from switch function on $ext
    // for example 'image/png'
    $filename = sha1(uniqid()) . '.' . $ext;
    $full_filename = $path . '/' . $filename;

    $stream = fopen('data://' . $contentType . ';base64,' . $imagedata, 'r');
    fseek($stream, 0);

    $opt = array(
        'fileUpload' => $stream,
        'acl' => AmazonS3::ACL_PUBLIC,
        'contentType' => $contentType
    );

    $s3 = new AmazonS3(AWS_KEY, AWS_SECRET_KEY);
    $response = $s3->create_object($bucket, $filename, $opt);

But the image that being saved is corrupted, in additionals this images or pdf have less bytes then orginal.

I need realy help 😀

  • 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-28T02:14:40+00:00Added an answer on May 28, 2026 at 2:14 am

    I’m not 100% sure this will work, but why not base64_decode the data back to binary, then write the data to a temporary file and send it to amazon from that location. Something like (untested):

    // in $imagedata is saved the string generated by service A
        $imagedata = base64_decode($_POST['serviceA_base64encodedfile']);
        if (!$imagedata){
            //Handle invalid base64 encoded data
        }
        // $contentType taken from switch function on $ext
        // for example 'image/png'
        $filename = sha1(uniqid()) . '.' . $ext;
        $full_filename = $path . '/' . $filename;
    
        $tmpfname = tempnam("/tmp", "image_to_upload");
        $populated =  file_put_contents($tmpfname,$imagedata);
        if (!$populated){
            //handle write failures 
        }
    
        $opt = array(
            'fileUpload'    => "/tmp/".$tmpfname,
            'acl'           => AmazonS3::ACL_PUBLIC,
            'contentType'   => $contentType
        );
    
        $s3 = new AmazonS3(AWS_KEY, AWS_SECRET_KEY);
        $response = $s3->create_object($bucket, $full_filename, $opt);
    

    I’m also presuming on the last call, that $full_filename is where you want to store the file on the s3 server… though you can just use $file_name.

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

Sidebar

Related Questions

I have a question about this formula from a book: EFW (cm,kg)= 10^(-1,7492+(0,166*BPD)+(0,046*AC)-(2,646*AC*BPD/1000)) The
I have a question about PHP's fopen() function (using php://output). Server A uses this
I have question about database optimizing, indexing. I have table that called projects and
I have question about sending emails from MVC3 application. I already read for exmple
I have question about python and sqlite3. I want to drop a table from
I have question about removing element from QList. myclass.h: class node2D : public QObject
I have question about try, catch and finally in Java. Consider the following scenario:
https://stackoverflow.com/a/64983/468251 - Hello, I have question about this code, how made that working with
I have question about parsing in Html helper : I have sth like: @foreach
I have question about clean thory in Python. When: @decorator_func def func(bla, alba): pass

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.