skip to Main Content

I can’t instanciate my $pemkey whit a relative path in my php code. When I try to instanciate my key with openssl_pkey_get_private the program doesn’t find it.

Here is my code :

$pemkey = openssl_pkey_get_private("file:///licensePrivateKey.pem");
if (! $pemkey ){
    echo "ERROR - Unable to load signing key.";
    die();
}

And here are my files :

  • folder
    • download_file.php
    • licensePrivateKey.pem

(Sorry can’t display images lol)

2

Answers


  1. Chosen as BEST ANSWER

    I solved the error by creating an absolute path with dirname(__FILE__) and and my file name. Here's the code :

    $path = dirname(__FILE__);
    $key = "licensePrivateKey.pem";
    $pemkey = openssl_pkey_get_private("file://".$path."/".$key);
    if (! $pemkey ){
        echo "ERROR - Unable to load signing key.";
        var_dump(openssl_error_string());
        die();
    }
    

  2. Check this note on PHP manual website https://www.php.net/manual/en/function.openssl-pkey-get-private.php#114998
    So you only have to concatenate "file://" with an existing path string in every case

    This looks like an absolute path /licensePrivateKey.pem
    You are using windows or unix system?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search