0

I am developing a security for my downloadable contents simply securing my download content like this.

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

Now I simply want to add 100 extra bytes (my custom details about that download file as per each download)

I want some way to add these 100 bytes after readfile($file); so downloaded file will have this extra information in it.

something like 
readfile($file) + myBytes()

I am new to PHP so please help me

Jack Gajanan
  • 1,596
  • 14
  • 18
  • 1
    This depends on the type of file you're downloading. Simply pasting extra bytes to the end of a file may render it effectively corrupt. – Alex Howansky Nov 02 '12 at 17:27
  • Maybe you should consider an encryption method with a secure key instead? http://stackoverflow.com/questions/2448256/php-mcrypt-encrypting-decrypting-file – Jeremy Harris Nov 02 '12 at 17:30

2 Answers2

2

Store your extra bytes in a file,variable,db or etc , and echo it after read file function

$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    readfile("extra_filename.ext");
    /* 
    //or use this
    $extra_bytes = "store extra bytes here";
    echo $extra_bytes;
    */
    exit;
}
Shahrokhian
  • 1,100
  • 13
  • 28
  • As simple as that – thanks to you to save my time Now I have developed my solution and my .net downloadable application is using this additional information for license verification – Jack Gajanan Nov 04 '12 at 05:47
  • You need to add the length of the extra bytes to the Content-Length header as well. – Nikhil Dabas Apr 11 '13 at 16:25
0

If you're only dealing with images, I'd recommend using steganography instead of just pasting bytes to the end of a file. Googling "php steganography library" turns up a few hits:

Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • I already have implemented steganography in Matlab but this is just a sample code from php.net actually my content is executer file which use this added information for license. – Jack Gajanan Nov 03 '12 at 02:50