Skip to content

Saving files with PHP scripts in local applications

When run in a desktop application, PHP code can access local files on the end user's computer, so it is possible to save and modify files locally. To load compiled files, please see this page.

Tip

Please refer to the Saving Files topic of the General Demonstration for live demonstrations and further explanation about saving local files with your ExeOutput for PHP apps.

Saving and storing files locally

PHP provides you with several ways to load and save files. In order to correctly save files, you must ensure that the user has permissions to write to files into the location you want.

For instance, a portable application is generally started from a USB stick and the folder that contains the EXE has write permissions. Your application will be able to save files in that folder. On the contrary, if your application is installed in the Program Files directory, it is generally not allowed to save files in the application's folder because of the Windows UAC feature.

Thus, ExeOutput for PHP provides you with a storage folder dedicated to your application. The absolute path to this folder can be retrieved with the following php command:

<?php $storagelocation = exo_getglobalvariable('HEPubStorageLocation', '');
echo $storagelocation;
?>
HEPubStorageLocation is a global variable defined by ExeOutput for PHP at runtime. The exo_getglobalvariable is a built-in PHP command defined by ExeOutput for PHP.

Tip

This folder is always available: you can use it to store the settings of your application. You may customize the name of the storage folder in ExeOutput by going to the "Output -> Output Settings" page.

PHP code: saving a file with fopen / fwrite

In compiled applications, you must pass absolute paths to the fopen PHP function.

<?php
$storagelocation = exo_getglobalvariable('HEPubStorageLocation', '');
$file = $storagelocation.'myfile1.txt';
$fp = fopen($file, "w") or die("Couldn't open $file for writing!");
fwrite($fp, $data) or die("Couldn't write values to file!"); 
fclose($fp); 
echo "Saved to $file successfully!";
?>

Tip

If you want to let your end users choose the path where the file should be saved, you can display a Save As dialog box.

Accessing Files in Compiled PHP Applications

How compiled PHP applications work

Working with PHP