Search code examples
phpregistrymime-types

Creating .reg files


I need export some data from a web application into Windows .reg files

I've managed to create the file and force a download.

$tmpfname = tempnam( sys_get_temp_dir(), "reg_".time().rand()."_" );
$tmpfpath = sys_get_temp_dir() . $tmpfpath;
$handle   = fopen( $tmpfname, "w" );

fwrite( $handle, $string );

header( 'Content-type: application/force-download' );
header( 'Content-Disposition: attachment; filename="'.time().'_import-'.$reg_folder.'.reg"' );
header( 'Content-Transfer-Encoding: binary' );
readfile( $tmpfpath );

fclose( $temp );
unlink( $tmpfpath );

The problem is such that once I run the downloaded .reg file, the registry editor opens as expected and askes about adding information[...]Are you sure?. Upon clicking yes I get the following error message

Cannot import [file]: The specified file is not a registry script. You can only import binary registry files from within the registry editor.

My file looks like this

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Martin Prikryl\WinSCP 2\Sessions\Test Site]

"HostName"="hostname"
"UserName"="username"
"PasswordPlan"="password"
<newline>
<newline>

I'm not entirely sure how to output the file in a binary format compatible with the registry editor.

The content type of registry files are "Registration Entries" according to Windows 7's Set Associations application but I've tried setting the content type to this in the script but it's not recognised.


Solution

  • I'm not entirely sure how to output the file in a binary format compatible with the registry editor.

    I think that is the cause of your problem. You need to create the file based on it's file-format specification. The mime-type is less important here, windows works more with file-endings (.reg) than mime-types. You just need to take care that you use the right mime-type with your file-format. For that you need to know the file-format, too.

    To which specific file-format specification are you referring to? Are you sure you want to create a binary .reg file and not a textual one?