0

I've developed a script that delivers files through secure URL, the goal is to mimic the private file hosting of Amazon S3. The user would send a request to the server which generates a URL which can be used to access the file. The generated URL expires after 2 hours from the time it was generated.

This is the decrypt part of the script:

$data = decrypt($_GET['data']);
    $data_json = json_decode($data,true);

    // print_r($data_json); die;
    $elapsed = strtotime('now') - $data_json['expire'];
    if ($elapsed > URLEXPIRE) {
        echo json_encode(array(
            'status'=>false,
            'error'=>'URL expired.',
            'debug'=>$data,
            'debug2'=>$_GET['data']
        ));
        die;
    }

    $object = OBJECTSTORE . $data_json['path'];
    header('X-Elapsed: ' . $elapsed);
    // header('Content-Type: '.get_mime_type($object));
    header('Content-Type: application/octet-stream');
    // header('Content-Disposition: attachment; filename=' . basename($object));
    if (strpos($data_json['path'],'?download'))
        header('Content-Disposition: attachment;');

    // echo file_get_contents($object);
    readfile($object);
    die;

During my test on my staging server, the script works fine. However, upon release in production when delivering large video files it seems to be slow. It takes a while to load the URL as a source of an HTML video tag. I don't know if this helps, but decrypt function uses openssl_decrypt using AES-256-CBC cipher.

Any idea of a more efficient approach to this?

Dominick Navarro
  • 752
  • 6
  • 20

1 Answers1

0

Verify that output buffer is off before the readfile() : add ob_end_clean(). Also, readfile is probably not efficient for large file. See Readfile() and large files

JayMore
  • 642
  • 6
  • 20