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

  • Home
  • SEARCH
  • 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 9249049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:05:46+00:00 2026-06-18T10:05:46+00:00

I have a PHP function that generates signed Amazon S3 URL’s as below: if(!function_exists(‘el_crypto_hmacSHA1’)){

  • 0

I have a PHP function that generates signed Amazon S3 URL’s as below:

  if(!function_exists('el_crypto_hmacSHA1')){
    /**
    * Calculate the HMAC SHA1 hash of a string.
    *
    * @param string $key The key to hash against
    * @param string $data The data to hash
    * @param int $blocksize Optional blocksize
    * @return string HMAC SHA1
    */
    function el_crypto_hmacSHA1($key, $data, $blocksize = 64) {
        if (strlen($key) > $blocksize) $key = pack('H*', sha1($key));
        $key = str_pad($key, $blocksize, chr(0x00));
        $ipad = str_repeat(chr(0x36), $blocksize);
        $opad = str_repeat(chr(0x5c), $blocksize);
        $hmac = pack( 'H*', sha1(
        ($key ^ $opad) . pack( 'H*', sha1(
          ($key ^ $ipad) . $data
        ))
      ));
        return base64_encode($hmac);
    }
  }

  if(!function_exists('el_s3_getTemporaryLink')){
    /**
    * Create temporary URLs to your protected Amazon S3 files.
    *
    * @param string $accessKey Your Amazon S3 access key
    * @param string $secretKey Your Amazon S3 secret key
    * @param string $bucket The bucket (bucket.s3.amazonaws.com)
    * @param string $path The target file path
    * @param int $expires In minutes
    * @return string Temporary Amazon S3 URL
    * @see http://awsdocs.s3.amazonaws.com/S3/20060301/s3-dg-20060301.pdf
    */

    function el_s3_getTemporaryLink($accessKey, $secretKey, $bucket, $path, $expires = 5) {
      // Calculate expiry time
      $expires = time() + intval(floatval($expires) * 60);
      // Fix the path; encode and sanitize
      $path = str_replace('%2F', '/', rawurlencode($path = ltrim($path, '/')));
      // Path for signature starts with the bucket
      $signpath = '/'. $bucket .'/'. $path;
      // S3 friendly string to sign
      $signsz = implode("\n", $pieces = array('GET', null, null, $expires, $signpath));
      // Calculate the hash
      $signature = el_crypto_hmacSHA1($secretKey, $signsz);
      // Glue the URL ...
      $url = sprintf('https://%s/%s', $bucket, $path);
      // ... to the query string ...
      $qs = http_build_query($pieces = array(
        'AWSAccessKeyId' => $accessKey,
        'Expires' => $expires,
        'Signature' => $signature,
      ));
      // ... and return the URL!
      return $url.'?'.$qs;
    }

    }

I have one page with a form button that i use to download a zip file:

<?php
// Grab the file url
$file_url = get_post_meta($post->ID, 'file_url', true);
// Grab just the filename with extension
$file_name = basename($file_url); 

// AWS details                  
$accessKey = "AKIAJJLX2F7GUDTQ23AA";
$secretKey = "<REMOVED>";
$bucket_name = "media.themixtapesite.com";

// Create new S3 Expiry URL
$download_url = el_s3_getTemporaryLink($accessKey, $secretKey, $bucket_name, $file_name);


if (is_user_logged_in()) { ?>
<div class="download_button_div">
<?php echo '<form action="'.$download_url.'" class="download_button_div">'; ?>
<!--Download counter-->
<input type="hidden" name="download_counter" value="<?php (int)$download_count = get_post_meta($post->ID, 'download_counter', true);
$download_count++;
update_post_meta($post->ID, 'download_counter', $download_count); ?>">
<button type='submit' class='download_button'>Download</button>
</form>

As you can see i simply set the Form Action to the URL of the file i want to download.
This works fine. It generates a signed, expiring S3 URL and downloads the file.

On another page, i use the same function, but a slightly different form and this is to download a single mp3 file:

<?php
// Grab the file url
$file_url = $mp3s;
// Grab just the filename with extension
$file_name = basename($file_url); 

// AWS details                  
$accessKey = "AKIAJJLX2F7GUDTQ23AA";
$secretKey = "<REMOVED>";
$bucket_name = "mixtape2.s3.amazonaws.com";

// Create new S3 Expiry URL
$download_url = el_s3_getTemporaryLink($accessKey, $secretKey, $bucket_name, $file_name);
?>

<!--Download individual MP3 file direct from player-->

<span style="right:27px; position:absolute;">
<form action="download_file.php" method="post" name="downloadform">
<input name="file_name" value="<?php echo basename($mp3s); ?>" type="hidden">
<input name="real_file" value="<?php echo $download_url; ?>" type="hidden">
<input type="image" src="download.jpg" border="0" width="17" alt="Download the MP3" />
</form>

As you can see on this form i submit the form using POST to another file called ‘download_file.php’ for processing. The ‘download_file.php’ is simply a file which forces the download of an mp3 file rather than opening it in the browser:

<?php
if(isset($_POST['file_name'])){
$player_file = $_POST['file_name'];
header('Content-type: audio/mpeg3');
header('Content-Disposition: attachment; filename="themixtapesite_'.$player_file.'"');
readfile($_POST['real_file']);
exit();
}
?>

The problem i have, on the second page (where i submit to ‘download_file.php), the URL generated doesn’t work. If i submit the form it simply downloads a file of 0kb.
If i view source then copy and paste the link in to the browser i get an S3 error message telling me the Signature Does Not Match.

I don’t understand why my first page works, but my second page doesn’t. The URL’s are both generated using the same function??

Any help appreciated.

  • 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-06-18T10:05:47+00:00Added an answer on June 18, 2026 at 10:05 am

    Thanks to @orique for spotting i had entered my bucket name incorrectly.
    Silly mistake… Coding blindness!

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

Sidebar

Related Questions

I have a php function that generates HTML code like below function j_uf_SomeFunction($some_var) {
I have a simple PHP function that generates the hexadecimal value for HTML colors
I have a php function that generates a comma separated list of Post IDs
If I have a PHP function that generates a random number, is it possible
I have a PHP function that extract dat of an invoice from DB. The
I have a PHP function that builds a JSON array via $jsonArray= array(); for
I have a PHP function that takes a variable number of arguments (using func_num_args()
I have a PHP function that requires can take 3 parameteres... I want to
I have a PHP function that I want to make available publically on the
Is it possible to have a PHP function that is both recursive and anonymous?

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.