Accessing Files in compiled PHP Applications

An application made with ExeOutput for PHP is not designed to work like on a web server: you may need to tweak your PHP code in order to create or access files on the end user's computer, or files listed in the File Manager that were compiled into your application EXE file by ExeOutput for PHP.

Tip

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

Accessing local files

By default, PHP scripts in your application made with ExeOutput for PHP can access any local file on the end user's computer. Absolute paths should be used. To access files shipped with your application, or compiled into it, please see below.

Accessing Source Files from the Internal Browser

Your application works as if it were server software, serving webpages and related files through the HTTP protocol. When the application is run, it creates its own custom pluggable protocol (similar to HTTP, FTP, etc.) in order to communicate with the Chromium rendering engine. In other words, an application works like a small server combined with a client (the main window allowing users to navigate through HTML and PHP pages). Your website is then available as if it were on a server, except that no Internet connection nor real server are necessary!

Tip

The internal browser is able to read files from the storage location only if URLs begin with http://heserver/ followed by the virtual path to the file.

Any compiled webpage in your application can be accessed from the application's browser using the URL: http://heserver/[virtual path]

When your application starts, the browser automatically navigates to your index page.

Accessing external files from the internal browser

Your application can automatically load external resource files (not compiled into the EXE file): you can for instance have image and media files outside the EXE file (in the same folder or a subfolder).

Examples:

  • If an HTML file references image1.png, the application will look for the image1.png file into its compiled data; if it is not found, it will try to locate it in the same folder as the EXE file (depending on the URI) and load it.

  • If you have image file in a subfolder, e.g. <img src="myfolder/my image.png" />, the application will expect the "my image.png" file to be in a subfolder named "myfolder" (if you leave the file external).

Note

External files have to be deployed with the application's EXE file, in their respective folders.

Accessing Source Files from PHP

ExeOutput for PHP makes the PHP runtime believe that PHP scripts and other compiled files are on the hard disk in a subfolder named Data available in the folder where the EXE is located, while they are actually in memory in the virtual storage.

For instance, if the path to your EXE file is e:\my folder\myprogram.exe, the path to the "Data" subfolder will be e:\my folder\data

You can consider that the "Data" subfolder plays the same role as the "www" folder in Apache.

Tip

The DOCUMENT_ROOT server variable contains the full path to the virtual "Data" folder used.

Info

You can also keep PHP files outside the EXE and place them directly into a real "Data" subfolder: see below.

For security reasons, you may want to choose a custom name for the "Data" virtual folder. ExeOutput for PHP's virtualization engine allows you to choose any virtual folder, even on a drive that does not exist! It is possible to configure an absolute path for the virtual "Data" subfolder where PHP can access scripts. To activate this feature, you have to go to "PHP Settings" => "Main Settings" and enable "Use an absolute path for the virtual "Data" subfolder". Then enter the path of your choice, for instance X:\Data.

Troubleshoot "file missing" errors or "php failed to open stream" warnings

Generally, ExeOutput for PHP will intercept all files requested by the PHP runtime and make them available. For instance, ExeOutput for PHP supports: include, require, include_once and require_once...

Warning

php's fopen cannot download and open files from URLs beginning withhttp://heserver/ as no real web server is used.

If PHP displays "missing file" errors and warnings like "php failed to open stream", you may need to mark files in the File Manager (select your files, click File Properties and turn this option on: Unpack the file(s) to virtual memory at startup) or use the ExeOutput-specific PHP function named exeoutput_unpackvirtualfile.

    string exo_unpackvirtualfile ( string $sourcepath , string $optionaldestpath )

$sourcepath is the virtual source path to the compiled file you want to unpack to memory.

$optionaldestpath is the absolute path to the destination virtual file. If you leave it blank, the path from $_SERVER['DOCUMENT_ROOT'] + the virtual path are used.

The function returns the absolute path to the virtual file (in UTF-8 format). This path can be used with php functions like fopen, file_exists, file_get_contents

Example: the following script makes the demo1.pdf available and displays its full (virtual) path and size:

<?php $filename = exo_unpackvirtualfile ('res\demo1.pdf', '');
echo $filename . ': ' . filesize($filename) . ' bytes';
?>

Accessing external files from PHP scripts

You can create a real Data subfolder where you can place any external or resource file that your application/php scripts require, such as database files, XML... Even php files may be placed in this folder and used in conjunction with the php scripts compiled in your application. We recommend you using the "External Files" feature of ExeOutput for PHP for managing external files automatically.

Important

External files have to be deployed with the application's EXE file, in the "Data" subfolder.

Warning

If the "Use an absolute path for the virtual "Data" subfolder" option ("PHP Settings" => "Main Settings") is enabled, you won't be able to access real files that you could have placed in a real "Data" subfolder...

For instance, we can have a file named "include1.txt" in the "Data" folder. The following script displays the contents of this file:

<?php 
$cont = file_get_contents($_SERVER['DOCUMENT_ROOT'].'\\include1.txt', FILE_USE_INCLUDE_PATH);
print($cont);
?>

About is_file and is_dir

The php standard functions is_file and is_dir were modified to handle virtual files and folders. They will return True if you check the existence of a virtual file, even if the latter is not already in memory, or of a virtual folder.

How to create and save files with PHP

How compiled PHP applications work